Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

str_replace with array in euqation type question

  • stevelegare
  • stevelegare's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
9 years 1 month ago #116952 by stevelegare
Hi all,
Is there a way to use str_replace like this in a equation type question ?
Code:
$supp = array('À', 'Á', 'Â');
$replace = array('A', 'A', 'A');
str_replace($supp, $replace, $str);
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 1 month ago #116969 by tpartner
There are a couple of problems with that...

1) str_replace cannot be applied to arrays.

2) You cannot define multiple variables within a single equation question.

What exactly are you trying to do?



.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • stevelegare
  • stevelegare's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
9 years 1 month ago #116981 by stevelegare
Replied by stevelegare on topic str_replace with array in euqation type question
Thanks for your reply.

I ask for first name and lastname. I want to store in a database field the answers with and without french characters (é,à,û, etc.).

Example : Éric Légaré
Field 1 = Éric Légaré
Field 2 = Eric Legare

Can I do that with equation question?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 1 month ago - 9 years 1 month ago #117064 by tpartner
Well,it seems that str_replace() doesn't work with arrays in Expression Manager.

You could do it with nested str_replace() statements but that gets real ugly with lots of characters. For example, this only does 2 characters:
Code:
str_replace("é", "e", str_replace("É", "E", str_replace("è", "e", str_replace("È", "E", q4_SQ001))))

I would be more inclined to do it with JavaScript. Adding this to the source of a multiple-short-text question will automatically load the second input without the French characters when you type or paste into the first input.
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify some elements
    var thisQuestion = $('#question{QID}');
    var input1 = $('li.question-item:eq(0) input.text', thisQuestion);
    var input2 = $('li.question-item:eq(1) input.text', thisQuestion);
 
    // Hide the second input
    //$('li.question-item:eq(1)', thisQuestion).hide();
 
    // Listener on the first input
    input1.on('keyup', function(e){
      repSpecChars();
    });
    input1.on('paste', function(e){
      setTimeout(function () {
        repSpecChars();
      }, 100);
    });
 
    // A function to replace special characters
    function repSpecChars(str) {    
      // The character arrays (must be same length
      var specialChars = ["ù", "Ù", "û", "Û", "à", "Â", "ç", "Ç", "é", "É", "è", "È", "ê", "Ê", "î", "Î", "ô", "Ô"];
      var nonSpecialChars = ["u", "U", "u", "U", "a", "A", "c", "C", "e", "E", "e", "E", "e", "E", "i", "I", "o", "O"];
 
      var input1Val = input1.val().toString();
 
      $(specialChars).each(function(i){
        input1Val = input1Val.replace(
          new RegExp(specialChars[i], 'g'),
          nonSpecialChars[i]
        );
      });
      input2.val(input1Val);
    }
    });
</script>


Here's a working survey:

File Attachment:

File Name: limesurvey...2221.lss
File Size:15 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 9 years 1 month ago by tpartner.
The following user(s) said Thank You: stevelegare
The topic has been locked.
  • stevelegare
  • stevelegare's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
9 years 1 month ago #117136 by stevelegare
Replied by stevelegare on topic str_replace with array in euqation type question
Hi TPartner,

This is really what I was looking for.

I would like to thank you for this incredible support you give to LS user.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 1 month ago - 9 years 1 month ago #117145 by DenisChenu
Replied by DenisChenu on topic str_replace with array in euqation type question

tpartner wrote: Well,it seems that str_replace() doesn't work with arrays in Expression Manager.
....

Feature request ?

Php str_replace is able to do it, and phpjs too : phpjs.org/functions/str_replace/ (see example 2).

But with :
Code:
str_replace(['À', 'Á', 'Â'], ['A', 'A', 'A'], QUESTION);

Need an real array system for EM.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Last edit: 9 years 1 month ago by DenisChenu.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose