php preg_replace find - 替换为 -

时间:2013-06-26 00:43:05

标签: php regex

我猜测&和;搞乱了我的搜索。当我搜索mdash它找到它但当然不会取代&要么 ;围绕它。

这是我尝试过的,我觉得应该有用(我也试过了很多其他的东西,但是......):

$description = "there. — with David";
$description = preg_replace("/\&mdash\;/", "—", $description);

我用Google搜索并搜索无效,所以现在我只是把头发拉出来......

谢谢!

加入:

我从网址获得$ description:?description = clay%252520%252526mdash%25253B%252520with%252520Veronica

$description = $_GET["description"];
$description=rawurldecode($description);
$description=rawurldecode($description);
$description=htmlentities($description);
$description=stripslashes($description);
$description = preg_replace("/\&mdash\;/", "—", $description);
echo $decription;

生产: 粘土和MDASH;与Veronica (没有''之间& mdash),因为它在这里转换为mdash

3 个答案:

答案 0 :(得分:2)

您不应使用preg_replace替换常量字符串。只需使用str_replace代替。

$description = str_replace("—","—", $description);

答案 1 :(得分:1)

你的&实际上是html实体&所以你需要替换

$description =str_replace("—", "—", $description);

html_entity_decode()是另一种方法

答案 2 :(得分:0)

  tim@roflcopter /tmp $ cat >blah.php
  <?php
  $description = "there. &MDASH; with David";
  $description = preg_replace("/\&mdash\;/i", "—", $description);
  echo $description;

  tim@roflcopter /tmp $ php blah.php
  there. — with David

看起来它有效,但是如果你打算使用preg_replace,你可能想要做一个不区分大小写的替换......这可能是你的搜索破坏问题。

相关问题