papaface Posted April 10, 2009 Share Posted April 10, 2009 Hello, I am trying to search a string for a particular occurrence however I want it to work regardless of spaces. This is the code I have currently: $string = "Hello I am papa_face."; $pos = strpos($string, "I am papa_face"); if ($pos !== false) echo "Contains the string."; else echo "Does NOT contain the string."; Obviously in this example the string does contain the search term so it returns "Contains the string." However if I change the string to: $string = "Hello I am papa_face."; $pos = strpos($string, "I am papa_face"); if ($pos !== false) echo "Contains the string."; else echo "Does NOT contain the string."; The string is not found. How can I get around this? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 As you stated in the subject of this topic, you can replace white space. You could try completely removing white space in both strings with something like this: $string = str_replace(' ', '', $string); //or with regex maybe? $string = preg_replace('/\s/', '', $string); Quote Link to comment Share on other sites More sharing options...
papaface Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks That worked Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Cool Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.