【问题标题】:How to remove query string from "href" attribute of anchor using php?如何使用php从锚的“href”属性中删除查询字符串?
【发布时间】:2016-11-04 15:08:46
【问题描述】:

我有这个字符串:

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

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

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

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

【问题讨论】:

  • 使用 PHP 有很多不同的方法可以做到这一点。你试过什么?
  • @JohnConde 我更新了我的问题!
  • 改用/\?[^"]+/
  • @Mohammad 谢谢但是我怎么能在里面使用"
  • 我已经提交了答案。

标签: php regex string preg-replace


【解决方案1】:

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

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

输出:&lt;a href="/title/tt3110958/" style="color:#666" &gt;

【讨论】:

    【解决方案2】:

    您可以为此使用str_replace()

    示例:

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

    【讨论】:

    • 它不会删除url的查询字符串。
    • 我认为$str 是未定义的变量。
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 2021-11-12
    • 2015-05-09
    • 2017-06-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多