Built-in function: between
| From: | Alex Baumann | Date: | Sat, 04 Sep 2010 19:52:49 +0000 |
| Subject: | Built-in function: between | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-49571@lists.php.net to get a copy of this message | ||
Hi!
Since I'm using this function in each project I'm working on, how about making it a built-in function?
function between ($text, $between, $and) {
$start = strpos($text, $between)+strlen($between);
if ($start === false) return "";
$end = strpos($text, $and, $start);
if ($end === false) return "";
return substr($text, $start, $end-$start);
}
Basicly this function returns a substring given in a text $text between two strings, $between and $and, occuring in $text.
Here are some examples:
echo between("PHP is awesome!", "PHP ", " awesome!");
is echo between("PHP is awesome!", "P", "o");
HP is awes echo between("PHP is awesome!", "PHP ", "great!");
As mentioned above, this function is very useful, for example when parsing formated input data like html files, and everyone I told about it adopted it and is very thankful. Thanks, Alex