//How to check if a string begins with another string$haystack = "valuehaystack";$needle = "value";if (strpos($haystack, $needle) === 0){echo "Found " . $needle . " at the beginning of " . $haystack . "!";}
$haystack = "valuehaystack";$needle = "haystack";
//If index of the needle plus the length of the needle is the same length as the entire haystack.if (strpos($haystack, $needle) + strlen($needle) === strlen($haystack)){echo "Found " . $needle . " at the end of " . $haystack . "!";}
With strccmp, etc...
is a===b? NOreturn false
With strpos
is a===b? NO -- iterating in haysackis a===c? NOis a===d? NO....is a===g? NOis a===g? NOis a===a? YESis 1===1? YES -- iterating in needleis 2===3? YESis 4===4? YES....is 8===8? YESis c===x? NO: oh God,is a===1? NO -- iterating in haysack againis a===2? NOis a===3? NOis a===4? NO....is a===x? NOis a===b? NOis a===b? NOis a===b? NOis a===b? NOis a===b? NOis a===b? NOis a===b? NO...... may many times......is a===b? NOis a===a? YES -- iterating in needle againis 1===1? YESis 2===3? YESis 4===4? YESis 8===8? YESis c===c? YES YES YES I have found the same string! yay!was it at position 0? NOPEWhat you mean NO? So the string I found is useless? YEs.Damn.return false
$content = "The main string to search";$search = "T";//For compare the begining string with case insensitive.if(stripos($content, $search) === 0) echo 'Yes';else echo 'No';
//For compare the begining string with case sensitive.if(strpos($content, $search) === 0) echo 'Yes';else echo 'No';
//For compare the ending string with case insensitive.if(stripos(strrev($content), strrev($search)) === 0) echo 'Yes';else echo 'No';
//For compare the ending string with case sensitive.if(strpos(strrev($content), strrev($search)) === 0) echo 'Yes';else echo 'No';
function startWith($haystack,$needle){if(substr($haystack,0, strlen($needle))===$needle)return true;}
function endWith($haystack,$needle){if(substr($haystack, -strlen($needle))===$needle)return true;}