today i wanted to create function in php where i have a string and i want to be able to search some variables in that string, if they are found, i wanted the function to return true or false if my characters are not found. i need to do this on my wallpaper website which uses php and mysql. so after much code and headace i figure out a simple way to do it.

finally, i figure it how to do it.

ok, here we go on this howto tutorial. here is my example:

im start with my scenario. i was getting referals from search engines such as google and yahoo. and the value of the referal url was :

$url = 'http://www.google.com/m?q=free wallpapers wallpaperama&lg=en&clien=firefox';

as you can see from the url, im interested in the keyword.. the &q= part

but then, i would get another referals like this:

1. $url= 'http://www.google.com/m?q=free wallpapers wallpaperama&lg=en&clien=firefox';
2. $url= 'http://www.google.com/m?sm=bo&q=free wallpapers wallpaperama&lg=en&clien=firefox';

as you can see from my two examples above, i have a real situation because of these:

1. in example one i would NOT be able capture the keyword after the q= because it doesnt start with &q= but instead it starts with ?q=
2. in example two i would also capture the keyword after the &q= because it matches my goal

so, if am going to have different ways to search the $url string im going to have to create an array so this is how i would go about declaring the values of my array.
$search['start'][] = '&q=';
$search['start'][] = '?q=';
as you can see on my $search array, i have two. so these are the two possibilities. if had i had more all i had to do is add more $search['start'][] =

ok, now that i have the value of what is to be search in my $url string i have to use some loop to go through the $search array. for that i will use the foreach loop, once i find it i will stop the loop and return it as true, otherwise, i want the function to return a false so this is how my function will look like:

function FindValue($url, $search){
	foreach($search as $key => $value){
		if(strstr($url,$search[$key])){
			return $search[$key];
		}
	}
	return false;
}


now that i have the function, i can find out if my url has a ?q= or a &q= and this is how i do it:

if($found = FindValue($url, $search['start'])){
	echo "Yes, I Found ". $found ." in ".$url; 
}else{
	echo "Not Found, Value is False";
}


you want to try it? ok, i have create a snippet for you so you can try it on your own. so the first thing to do is to copy and past the following code into a blank notepad or your favorite text editor. NOTE: you must have PHP on your website.

<?
###################################################################
######### SEARCH AND FIND SCRIPT BY WALLPAPERAMA.COM ##############
###################################################################

# This Function Looks for the value in a string, it if finds it, 
# it will return the value of the found varialble as true
function FindValue($url, $search){
	foreach($search as $key => $value){
		//echo $search[$key].' in '.$url.'<br>';
		if(strstr($url,$search[$key])){
			return $search[$key];
		}
	}
	return false;
}
# URL is an the string where i want to look into to find what im looking for
$url = 'http://www.wallpaperama.com/?q=i am looking for this';
	
# this array is for the values you want to search in your $url string, it will search for these strings in the $url
$search['start'][] = '&q=';
$search['start'][] = '?q=';

# This determines if the functions returns a true value at the same time, 
# it declares the value of $found which will be whatever value in the $search array
if( $found = FindValue($url, $search['start'])){
	echo "i found it ".$found; 
}else{

	echo "not found";
}
?>


once you have copy and paste the code above, save the file as: wallpaperama.php and upload to your php website, then open it with your browser and you will see it work in action. try playing with it by changing different values to see if you get the results you wanted.

hope this helps. If you find this script helpful. all we ask that you please provide comments and please do not remove the WALLPAPERAMA.COM part on the script.

Wallpaperama Team.