www.webdeveloper.com
Results 1 to 6 of 6

Thread: replace any kind of quotes in a textarea

  1. #1
    Join Date
    Dec 2009
    Location
    Budapest, HUN
    Posts
    6

    replace any kind of quotes in a textarea

    Hey all! =)

    I'd like to replace ["] and ['] in a textarea before POST with PHP. So if u type in for example: ondblclick="document.getElementById('this.id')", and u do a POST after it, it replaces the quotes with special characterchains, which the user can define.

    I have the solution for only the ["] or ['], but if I want to replace both, it just doesn't work, so my PHP back-end is screwed also... =) Help pls! This is my graduation work... =)

    Thanks! =)

    Bence

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var userChar='[]';
    var quotes = /("|')/ig;
    txt=txt.replace(quotes, userChar);

  3. #3
    Join Date
    Dec 2009
    Location
    Budapest, HUN
    Posts
    6
    Yes, thanks, I'll memorize it, coz it's usefull too. But I'm developing a webeditor for the web... So I have to define them one by one... Which means I was thinking of something like this:

    var text="kjdfskjbh\'jklbhjk\"de";
    str=text.split("");
    var finaltext="";
    for(x in str){
    if(str[x]=="\'"){
    str[x]="Q$1Q";
    finaltext+=str[x];
    } else {
    if(str[x]=="\""){
    str[x]="Q$2Q";
    finaltext+=str[x];
    } else finaltext+=str[x];
    }; //else
    }; //for

    Is this correct, or it has it's disadvantage? =) Thanks ppl! =)

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    You need the equivalent of php's str_replace

  5. #5
    Join Date
    Dec 2009
    Location
    Budapest, HUN
    Posts
    6
    yea I thought of that too, but I want to keep the code as simple as possible. I've replaced the quotes, and it POSTs it also, but the PHP isn't working if I'm trying to re-replace:

    $str=str_replace(array("\'","\""), array("Q$1Q","Q$2Q"),$content);

    and it doesn't work if I want to put it in a file either.

    file_put_content($filename, $str);

    It creates the file, but it doesn't put the strings in it...
    By the way! Is it possible to somehow "send" arrays from JavaScript to PHP?

    Sorry for my questions, but I'm not a webprogrammer... =)

    Thanks! =)

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by benkus View Post
    By the way! Is it possible to somehow "send" arrays from JavaScript to PHP?
    yes.
    use the JSON tool. it's part of firefox and chrome, and is a free download for IE compatibility.

    you convert the array to JSON:
    Code:
    var json=JSON.stringify(array);
    and in php, you can easily convert the JSON to a phpArray:
    Code:
    $myArray=json_decode($json);
    i've left out your POST steps, but this is what to do on each side...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles