Starts with
Tests if a text starts with an given string.
/** * StartsWith * Tests if a text starts with an given string. * * @param string * @param string * @return bool */ function StartsWith($Haystack, $Needle){ // Recommended version, using strpos return strpos($Haystack, $Needle) === 0; } // Another way, using substr function StartsWithOld($Haystack, $Needle){ return substr($Haystack, 0, strlen($Needle)) == $Needle; }
Author:
Jonas
License:
Public Domain
Language:
PHP
Created:
03/28/2006
Updated:
09/25/2007
Tags:
string functions
Related links:
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
how i can greb html who contenet sarted whit any tag and ended any tag!
for example:
i want grab all content in this tag
<div class=it>
mnmnmnmfdgdsfgdfsg
dsfgdsfgdfsgfd
sgsfdgsdfg
dfgfdgdsfg
fdsgdsg
dfgsdfgsd
<div class=ip>
That should make the function case-insensitive.
function startsWith($h, $n){
return (false !== ($i = strrpos($h, $n)) &&
$i === strlen($h) - strlen($n));
}
// Variables' ids shortened
function starts_with($h,$n) { // haystack, needle
return strpos($h,$n) === 0;
}
I wonder why...
A year ago my programming skills were not the
best... Now I updated the snippet.
Jonas
function starts_with($str, $src){
return substr($src, 0, strlen($str))==$str;
}
Instead of: return true ? true : false;