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;
}
?>
Filed under: PHP Functions 1 Comment
11Nov/102

Function to get UNIX time

This will return a UNIX style time stamp...

<?php
function get_unix_time()
{
	return time();
}
?>
Filed under: PHP Functions 2 Comments
11Oct/100

Function to compare 2 string values

This function will compare 2 values,
make sure they are strings,
and make sure they are not empty

here is the code.

<?php
 
function str_compare($str1 , $str2)
{
	// we are converting the values to strings...
	// comment out the following 2 lines 
	// if you wish to check other value types.
	$str1 = (string)$str1;
	$str2 = (string)$str2;
 
	/// we are checking to see that both values are not empty.
	/// if one of the strings is empty, we return false.
	if (empty($str1) || empty($str2))
	{
		return false;
	}
	// the following line is the same like doing
	/*
	if($str1 === $str2)
	{
		$same = true;
	}
	else
	{
		$same = false;
	}
	*/
	$same = (($str1 === $str2)? true : false);
 
	return $same;
}
 
?>

enjoy learning, and please,
feel free to comment, or ask anything you want to know