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!

Recommended Answers

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

Jump to Post

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.

Jump to Post

All 6 Replies

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

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

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.

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?

Right.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.