note 107287 deleted from function.substr by peehaa
| From: | peehaa@php.net | Date: | Mon, 28 Nov 2016 19:46:38 +0000 |
| Subject: | note 107287 deleted from function.substr by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207717@lists.php.net to get a copy of this message | ||
Note Submitter: maxim at inbox dot ru
----
Here is a recursion function to get parts of passed string which are in a char. Func looks pretty,
and works fast, tell me please if you find more opt way.
<?php
$s = "info= &make&,endvcc &new& &another&info";
echo str_cut($s,"&",",");
//output:
//make,new,another
function str_cut($s,$a,$d="")
{
$f=strpos($s,$a)+1;
$l=strpos($s,$a,$f);
$out= substr($s,$f,$l-$f);
if
(strpos($s,$a,$l+1)!==false)
{$s=substr($s,$l+1);$out.=$d.str_cut($s,$a,$d);}
return $out;
}
?>
it is possible to output in array, you have to returns a var as an array.
Also you can add an extra needle, which would be compare with the end of strings :
replace to
$l=strpos($s,$b,$f)-$f;
and dont forget to pass into recursion call $b value, but i did like that
$b=func_get_arg(func_num_args()-1);
care! the last argument should be $a, because $b getting last arg. like that:
function str_cut($s,$d="",$a)
but i think this method of getting $b not the best way for perfomance.
Sorry for my English.