Php simple free text search

Asked By 10 points N/A Posted on -
qa-featured

Hi there,

I am in a bit of a dilemna, I simply have no idea how to search for a desired text in php, all I need is the code for php simple free text search, for this code example.

<?php

$text ="hello World!";

if ($text contains "world") {
echo "True";
}

?>

Any help would be greatly appreaciated

SHARE
Best Answer by Nathan H Underwood
Answered By 45 points N/A #132526

Php simple free text search

qa-featured

Hello there Zubin,

There are a lot of ways to get what you want. All you need to do is to familiarize the string methods available in php. You may try the following methods that I define next to this.

Using strpos() or stripos()

if(stripos($text, "world") !== false) {

    echo "True";

}

Using strstr() or stristr()

if(strstr($text,"world"))   

{

echo "True";

}

You may use these methods but it is more convenient to use strpos() if you want to find the text because it can give less trouble.

Regards Ronald

Best Answer
Best Answer
Answered By 60 points N/A #132527

Php simple free text search

qa-featured

Hello Zubin Lennon, 

This might be what you are looking for:
 
<?php
 
$text = 'This is a Simple text.';
 
// this echoes "is is a Simple text." because 'i' is matched first
echo strpbrk($text, 'mi');
 
// this echoes "Simple text." because chars are case sensitive
echo strpbrk($text, 'S');
?>
 
Is it?
 
Or maybe this:
 
<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);
 
// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>
 
Or even this
 
<?php
$email  = '[email protected]';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
 
$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name
?>
 
You can read all about them in the documentation here:
 
 
Or check here:
 
 
Regards!
Nathan H Underwood

 

Related Questions