如何使用 php 从锚点的 "href" 属性中删除查询字符串?

How to remove query string from "href" attribute of anchor using php?

我有这个字符串:

$string = '<a href="/title/tt3110958/?ref_=nm_flmg_act_2" style="color:#666" >';

如何删除字符串中从 ?" 的所有单词。 我试试这个:

$result = preg_replace("/\?.+/", "", $string);

但它删除了 ? 之后的所有单词!!!

你可以使用 str_replace()

示例:

$myStr = "foo? bar???";
$myStr = str_replace("?","\"",$myStr);

像下面这样使用/\?[^\"]+/

<?php
$string = '<a href="/title/tt3110958/?ref_=nm_flmg_act_2" style="color:#666" >';
$result = preg_replace("/\?[^\"]+/", "", $string);
echo $result;
?>

输出:<a href="/title/tt3110958/" style="color:#666" >