Jump to content


Photo

searching a word in a string


  • Please log in to reply
3 replies to this topic

#1 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 07 September 2006 - 01:04 AM

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?

#2 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 07 September 2006 - 01:14 AM

strpos would probalby work for you, but the pros would go with some regex
http://us2.php.net/m...tion.strpos.php

check out the string functions in php
http://us2.php.net/m...ref.strings.php

#3 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 September 2006 - 01:15 AM

Here is one way:

<?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";
}
?>

Then you can add words to the array that you want to check for.

#4 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 07 September 2006 - 01:19 AM

thanks a lot :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users