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
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!");
preg_match('/PHP (.*) swesome/', 'PHP is awesome', $matches);
works nicely, we won't add special purpose functions for things that can
easily be done already.
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.
For parsing DOM or tidy are better.
johannes
Am 05.09.2010 um 15:06 schrieb Johannes Schlüter:
preg_match('/PHP (.*) swesome/', 'PHP is awesome', $matches);
preg_match should be slower?
Am 05.09.2010 um 15:06 schrieb Johannes Schlüter:
preg_match('/PHP (.*) swesome/', 'PHP is awesome', $matches);
preg_match should be slower?
Was that a question or a statement?
I doubt it is really measurable and not worth adding another special
case function. We have enough of them.
johannes
Perhaps a better idea would be to group the ones that we already have under
a common namespace. But this is no news, though.
Adrian
2010/9/30 Johannes Schlüter johannes@schlueters.de
Am 05.09.2010 um 15:06 schrieb Johannes Schlüter:
preg_match('/PHP (.*) swesome/', 'PHP is awesome', $matches);
preg_match should be slower?
Was that a question or a statement?
I doubt it is really measurable and not worth adding another special
case function. We have enough of them.johannes
Perhaps a better idea would be to group the ones that we already have
under a common namespace. But this is no news, though.
Even then it would be unlikely that we add another special purpose
function. This would have to be an important feature, ringing huge
improvements in performance versus existing solutions (like preg_math)
johannes
Completely agree. Adding another specialized string function wouldn't be
worth in this case. I was just trying to divert the attention :) to PHP
(still) not using namespaced functionalities even where it makes sense the
most (text / string, array etc.)
2010/9/30 Johannes Schlüter johannes@schlueters.de
Perhaps a better idea would be to group the ones that we already have
under a common namespace. But this is no news, though.Even then it would be unlikely that we add another special purpose
function. This would have to be an important feature, ringing huge
improvements in performance versus existing solutions (like preg_math)johannes
Perhaps a better idea would be to group the ones that we already have
under a common namespace. But this is no news, though.Even then it would be unlikely that we add another special purpose
function. This would have to be an important feature, ringing huge
improvements in performance versus existing solutions (like preg_math)
I can see (and always wish for) in_string() existing, but that's more for naming reasons. Choosing strpos()
or strstr()
or some other random string function for this common task feels dirty. Imagine if we had to use arrpos() instead of in_array()
;) Also, since strpos()
is our official method, we must worry about 0 == false.
Sorry for hijacking this thread, but it feels related.
As for between(), creating a cookbook of common tasks (and inserting them into the manual) sounds reasonable. For example, we did something like this for the plethora of array sorting functions:
Regards,
Philip