let's say i have
$variable = 'something&list=anot开发者_开发问答herthing';
how can i remove all starting with
&list=
Where "anotherthing" it is not the same, everytime.
strrpos and substr will give you the results you want.
$pos = strrpos($var, '&list=');
echo substr($var, 0, $pos);
Returns:
something
And $pos + 6 (length of &list=
)
echo substr($var, 0, $pos + 5);
Returns
something&list=
What you really should do is:
parse_str($variable, $foo);
foreach($foo as $k=>$v) {
// do something with your somethings
if ($k == "list") {
unset($foo[$k]);
}
}
// convert the $foo array back to a string
hope this helps
** let me just add a slight disclaimer **
This answer was provided based on the string the user gave ... It may not be fit for purpose on a normal string that can't be parsed properly with parse_str
http://www.php.net/manual/en/function.parse-str.php
You could explode it into an array and then set the variable like so:
$variable = explode('&list','something&list=anotherthing');
$variable = $variable[0];
精彩评论