Skip to main content

Replace ="Text” with =\"Text\” using Preg_Replace



I would like to replace the following:





="Dark4Red"





with





=\"Dark4Red\"





The = sign and double quotations are fixed ="..." I just need to add slashes before double quotations.







$content = preg_replace('/="[^a-zA-Z#0-9]"/', '=\"[^a-zA-Z#0-9]\"', $line);







The above solution didn't work. Any idea?


Comments

  1. How about addslashes?

    That aside, you appear to have not read any of the examples on preg_replace's manual page - if you had, you'd have known that you capture a subpattern (in this case the contents of the quotes) with parentheses and use $1 to put them in the replacement string.

    ReplyDelete
  2. You can use

    $str = '"Text"';
    str_replace('"','\"', $str);

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?