gardan06 Posted September 7, 2006 Share Posted September 7, 2006 for example:$string = "The quick brown fox jumps over the lazy dog";what command should i use to know whether the word "jumps" exists in that string? Link to comment https://forums.phpfreaks.com/topic/19969-searching-a-word-in-a-string/ Share on other sites More sharing options...
emehrkay Posted September 7, 2006 Share Posted September 7, 2006 strpos would probalby work for you, but the pros would go with some regexhttp://us2.php.net/manual/en/function.strpos.phpcheck out the string functions in phphttp://us2.php.net/manual/en/ref.strings.php Link to comment https://forums.phpfreaks.com/topic/19969-searching-a-word-in-a-string/#findComment-87518 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 Here is one way:[code]<?php$word_filter = array( 'jump',);$post = 'The quick brown fox jumps over the lazy dog';function has_bad_words($post){ global $word_filter; $split = preg_split("#\s+#", $post, -1, PREG_SPLIT_NO_EMPTY); if (is_array($split)) { foreach ($split as $post_word) { if (in_array($post_word, $word_filter)) { return true; } } } return false;}if (has_bad_words($post)){ echo "The string contains jump";}else{ echo "The string doesn't contain jump";}?>[/code]Then you can add words to the array that you want to check for. Link to comment https://forums.phpfreaks.com/topic/19969-searching-a-word-in-a-string/#findComment-87519 Share on other sites More sharing options...
gardan06 Posted September 7, 2006 Author Share Posted September 7, 2006 thanks a lot :) Link to comment https://forums.phpfreaks.com/topic/19969-searching-a-word-in-a-string/#findComment-87522 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.