3w PHP Your Source for PHP Source Code

11Nov/101

Function to check if a string exist in another string

Option 1
We call for the position of the string, if it dose not exist, we get FALSE, which is what we check for in option 1.

<?php
function match_str_part($main_str , $needle)
{
	if (empty($main_str) || empty($needle))
	{
		return false;
	}
 
	if (strpos($main_str , $needle) !== FALSE)
	{
		return true;
	}
	return false;
}
?>

Option 2
Simply checking if the string exist as part of another.

<?php
function match_str_part($main_str , $needle)
{
	if (empty($main_str) || empty($needle))
	{
		return false;
	}
 
	if (strstr($main_str , $needle))
	{
		return true;
	}
	return false;
}
?>
Comments (1) Trackbacks (0)
  1. Love your site man keep up the good work


Leave a comment

(required)

No trackbacks yet.