note 107287 added to function.substr
| From: | maxim at inbox dot ru | Date: | Fri, 27 Jan 2012 16:43:15 +0000 |
| Subject: | note 107287 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185766@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 93.93.89.7
Probable Submitter: 95.221.37.251
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/107287
Del: integrated -- https://master.php.net/note/delete/107287/integrated
Del: useless -- https://master.php.net/note/delete/107287/useless
Del: bad code -- https://master.php.net/note/delete/107287/bad+code
Del: spam -- https://master.php.net/note/delete/107287/spam
Del: non-english -- https://master.php.net/note/delete/107287/non-english
Del: in docs -- https://master.php.net/note/delete/107287/in+docs
Del: other reasons-- https://master.php.net/note/delete/107287
Reject -- https://master.php.net/note/reject/107287
Search -- https://master.php.net/manage/user-notes.php