954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

preg_replace , escape quote

How do I deal with quotes here?
What i want is /[^a-zA-Z0-9-_.,!()'\/"]/ but how do i deal the quotes?

$pattern = '/[^a-zA-Z0-9-_.,!()'"\/' . $whatever . ']/';
return preg_replace($pattern, '',  $value);


Cheers!

woopi
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Escape it with a backslash, read more in the manual or on this website .

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hi pritaeas , thank you.
I understand that but how do i combine escape both for php and preg_replace?

woopi
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

For the backslash it would look like this:

preg_match('%[^a-zA-Z0-9-_.,!()\'"\\\\/]%s', $subject)

As mentioned in the first note in the first link, first escape for the regex, and then for the php string.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Sorry for not really understanding ...
so a single backslash will escape single quote for php in this case and preg_match won't bother with it?

woopi
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Right.

pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thank you!

woopi
Newbie Poster
8 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: