Replacing 2 characters with with preg_replace

Thread Solved

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Replacing 2 characters with with preg_replace

 
0
  #1
Jan 15th, 2008
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:

  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\")
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,738
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Replacing 2 characters with with preg_replace

 
1
  #2
Jan 15th, 2008
Umm.. I am not sure about preg_replace. But you can do the same with str_replace!
  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
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Replacing 2 characters with with preg_replace

 
1
  #3
Jan 15th, 2008
you dont need str_replace inside str_replace etc etc.

use two arrays

  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).
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,401
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Replacing 2 characters with with preg_replace

 
0
  #4
Jan 15th, 2008
... I'm not entirely sure why'd you write your own function when the built-in addslashes function works perfectly well.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 57
Reputation: Walkere is an unknown quantity at this point 
Solved Threads: 5
Walkere Walkere is offline Offline
Junior Poster in Training

Re: Replacing 2 characters with with preg_replace

 
1
  #5
Jan 15th, 2008
Originally Posted by ShawnCplus View Post
... 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...
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Replacing 2 characters with with preg_replace

 
0
  #6
Jan 16th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC