943,724 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 5811
  • PHP RSS
Jan 15th, 2008
0

Replacing 2 characters with with preg_replace

Expand Post »
Hi there

I'm using preg_replace to replace ' and " with a backslash in front of them so that I don't get problems with my MySQL INSERT queries. But at the moment I can only replace them with one charater as below:

php Syntax (Toggle Plain Text)
  1. $unedited = $_POST['content'];
  2. $content = preg_replace("/['|\"]/", "\'", $unedited);

At the moment that just replaces both ' and " with ' which isn't ideal. Can anyone tell me how I'd be able to replace them with the respective charater. (e.g. ' would be replaced with \' and " would be replaced with\")
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Jan 15th, 2008
1

Re: Replacing 2 characters with with preg_replace

Umm.. I am not sure about preg_replace. But you can do the same with str_replace!
php Syntax (Toggle Plain Text)
  1. <?php
  2. $txt="' is a single quote \" is a double quote.I want to replace ' with \' and \" with \" ";
  3. echo "Actual text: " .$txt."<br />";
  4. $x=str_replace('"','\"',str_replace("'","\'",$txt));
  5. echo "Replaced text: ". $x;
  6. ?>

Hope it helps!
Naveen
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jan 15th, 2008
1

Re: Replacing 2 characters with with preg_replace

you dont need str_replace inside str_replace etc etc.

use two arrays

PHP Syntax (Toggle Plain Text)
  1. $your_txt = "Hello 'WORLD'";
  2. $txt = array("'", "\"");
  3. $txt_replace = array("\'", "\\\"");
  4. $x=str_replace($txt, $txt_replace, $your_txt);
  5. echo $x;

this way you can have two arrays and no messy inner replacements inside replacements.

although nav33n may have shown you his way as at first it appears easier (both do the same trick).
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Jan 15th, 2008
0

Re: Replacing 2 characters with with preg_replace

... I'm not entirely sure why'd you write your own function when the built-in addslashes function works perfectly well.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jan 15th, 2008
1

Re: Replacing 2 characters with with preg_replace

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
... I'm not entirely sure why'd you write your own function when the built-in addslashes function works perfectly well.
Aye, I was wondering the same thing.

addslashes() is slightly different - it escapes a couple of extra characters (including '\' and NUL). However, these (especially \) need to be escaped anyway.

So, to fill in the original example...
PHP Syntax (Toggle Plain Text)
  1. $unedited = $_POST['content'];
  2. $content = addslashes($unedited);

However, you might be looking to learn how to use preg_replace... so we'll go back to the original example.

You can use preg_replace() like Fungus did with str_replace - sending an array as the search and replacement parameters.

The problem with your example is that you're only presenting one thing for the replacement parameter - "\'".

In order to replace ' with \' and " with \" you need to do one of a few things - perform two preg_replaces (one for each replacement), use arrays within the preg_replace, or use a variable in the regex to print the quote based on what the search found. I'm not that good with regex, so I'm not sure if you can do that... but I think you can.

Anyhow, here's an example of how you could use arrays to perform the desired function with preg_replace.
PHP Syntax (Toggle Plain Text)
  1. $string = "\"You're not very wise,\" said the wise man.";
  2.  
  3. $string = preg_replace(array("/'/", '/"/'), array("\'", '\"'), $string);
  4. echo $string;

That appears to escape both the single and double quotes just fine.

Good luck,
- Walkere
Reputation Points: 29
Solved Threads: 5
Junior Poster in Training
Walkere is offline Offline
57 posts
since Jan 2008
Jan 16th, 2008
0

Re: Replacing 2 characters with with preg_replace

All I can say is WOW. I have to admit that I'm somewhat of a noob to PHP and that addslashes function is the best thing since sliced bread

Thanks to all of you for your response and especially to you Walkere (extra rep for you ) for taking the time to give an example using pre_replace. Much appreciated.
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need help NEWSLETTER
Next Thread in PHP Forum Timeline: creating pdf in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC