- Forums
- PHP
- How To Find In String Finding Value In String With Php
this page will show you how you can find a string within another string value using php code. i will give you examples and how to exactly search and find certain text inside a string [1116], Last Updated: Sat May 18, 2024
Gustavo
Mon Mar 03, 2008
12 Comments
27713 Visits
Hi My name is Gustavo,
i recently started to code some pages with php.
one of the challenges i have come up with is how do i find a particular string withing a string variable.
for example, lets say i have this string:
$string = 'wallpaperama has cool and amazing wallpapers';so lets say for example that i want to check if the $string variable contains the word 'wallpaperama'
as humans, its obvious that it does because you can see it. but i want my PHP script to catch it for me.
i found two ways to do it with two functions in php, here they are:
strpos() = string position
and
strstr() = Find first occurrence of a string
gathering from some of my replies here, i guess strpos is better
so these are some examples on how i used these functions:
Example 1. In this example, im going to use the strpos() function to lookup my string, and it should find it.
$string = 'wallpaperama has cool and amazing wallpapers';
$find = 'wallpaper';
if(strpos($string, $find) ===false){
echo '<b>strpos()</b><BR>NO - Did not find <u style="color:red;">'.$find.'</u> in <b>'.$string.'</b>';
}else{
echo '<b>strpos()</b><BR>YES - Found <u style="color:green;">'.$find.'</u> in <b>'.$string.'</b>';
}
OUTPUT:
strpos()
YES - Found wallpaper in wallpaperama has cool and amazing wallpapers
Example 2: In this example, im going to use the strpos() function to lookup my string, and it SHOULD NOT find it.
$string = 'wallpaperama has cool and amazing wallpapers';
$find = 'buzz';
if(strpos($string, $find) ===false){
echo '<b>strpos()</b><BR>NO - Did not find <u style="color:red;">'.$find.'</u> in <b>'.$string.'</b>';
}else{
echo '<b>strpos()</b><BR>YES - Found <u style="color:green;">'.$find.'</u> in <b>'.$string.'</b>';
}
OUTPUT:
strpos()
NO - Did not find buzz in wallpaperama has cool and amazing wallpapers
Example 3. In this example, im going to use the strstr() function to look up my string, and it should find it.
$string = 'wallpaperama has cool and amazing wallpapers';
$find = 'wallpaperama';
if(strstr($string, $find) ===false){
echo '<b>strstr()</b><BR>NO - Did not find <u style="color:red;">'.$find.'</u> in <b>'.$string.'</b>';
}else{
echo '<b>strstr()</b><BR>YES - Found <u style="color:green;">'.$find.'</u> in <b>'.$string.'</b>';
}
OUTPUT:
strstr()
YES - Found wallpaperama in wallpaperama has cool and amazing wallpapers
Example 4: In this example, im going to use the strstr() function to lookup my string, and it SHOULD NOT find it.
$string = 'wallpaperama has cool and amazing wallpapers';
$find = 'buzz';
if(strstr($string, $find) ===false){
echo '<b>strstr()</b><BR>NO - Did not find <u style="color:red;">'.$find.'</u> in <b>'.$string.'</b>';
}else{
echo '<b>strstr()</b><BR>YES - Found <u style="color:green;">'.$find.'</u> in <b>'.$string.'</b>';
}
OUTPUT:
strstr()
NO - Did not find buzz in wallpaperama has cool and amazing wallpapers
so as you can see, you can find any string you want within another string.
hope that help. and thanks for your comments. I am from El Salvador. Its a small country in Central America. So i apologize if my English is not perfect.