php str_replace其中的单词以

时间:2011-07-10 16:02:57

标签: php replace string

让我说我有

  

$ variable =   '东西&安培;列表= anotherthing';

如何删除所有以

开头的内容
  

&安培;列表=

每次“anotherthing”都不一样。

3 个答案:

答案 0 :(得分:2)

strrpossubstr会为您提供所需的结果。

$pos = strrpos($var, '&list=');

echo substr($var, 0, $pos);

返回:

something

$ pos + 6(&list=的长度)

echo substr($var, 0, $pos + 5);

返回

something&list=

答案 1 :(得分:2)

你真正应该做的是:

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 

希望这会有所帮助

**让我添加一个轻微的免责声明**

这个答案是根据用户提供的字符串提供的...它可能不适合用于无法使用parse_str正确解析的普通字符串

http://www.php.net/manual/en/function.parse-str.php

答案 2 :(得分:1)

您可以将其分解为数组,然后像这样设置变量:

$variable = explode('&list','something&list=anotherthing');
$variable = $variable[0];
相关问题