开发者

php str_replace where word starting with

开发者 https://www.devze.com 2023-03-19 00:29 出处:网络
let\'s say i have $variable = \'something&list=anot开发者_开发问答herthing\'; how can i remove all starting with

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消