tags:

views:

380

answers:

3

Hi

How can I check a string in php for specific characters such as '#' or '\'?

I don't really want to use replace, just return true or false.

Thanks

+3  A: 

use the function strstr http://us2.php.net/manual/en/function.strstr.php Returns part of haystack string from the first occurrence of needle to the end of haystack

Note: If you only want to determine if a particular needle occurs within haystack , use the faster and less memory intensive function strpos() instead.

Lizard
Cheers it was strpos I was after :)
ing0
+2  A: 

You can use the strpos function, if you only want to know if a string contains another one (the content of your questions seems to indicate that, even if your title says "remove").

Note : don't forget to use the !== or === operator, as the function can return 0 or false, and those have different meaning.


If you want to "remove" characters, str_replace or strtr might do the trick.

Pascal MARTIN
Thanks for your help
ing0
+3  A: 

You can do something like this:

if (strpos($string, '#') !== false || strpos($string, '\') !== false) {
    // One of those two characters is in the string.
}

Note in particular the !== syntax, which differentiates between false (meaning the character isn't found) and 0 (meaning it was found at position 0).

VoteyDisciple
Thanks for your help
ing0