【问题标题】:php remove url but keep anchorphp删除url但保留锚点
【发布时间】:2018-06-16 16:21:59
【问题描述】:

我有一些长文本的链接:

<a class="class1" href="https://www.test.com/de/de/products/2018/street/bike/details.html#2"><sup>2</sup></a>

我喜欢删除 href 但保留这样的锚点:

<a class="class1" href="#2"><sup>2</sup></a>

网址并不总是相同,因此“de/de/products/2018/street/bike/”这一部分会有所不同。而且锚的数量也各不相同。

这可能吗?

非常感谢您的帮助!

【问题讨论】:

  • 理想的做法是始终发布问题并展示您尝试过的内容或至少在 Google 上搜索过的内容。你可以使用codepen、jsfiddle等
  • 如果您不能使用jquery,请尝试使用explode

标签: php url replace


【解决方案1】:

好的,最后真的很简单。

因为我可以删除我使用的代码中的所有href

$dateninhalt = preg_replace('/http[\s\S]+?.html/', '', $dateninhalt);

所以从链接

<a class="class1" href="https://www.test.com/de/de/products/2018/street/bike/details.html#2"><sup>2</sup></a>

从 "http" 到 ".html" 的所有内容,包括这两个字符串,都被替换为空 ('')。

【讨论】:

    【解决方案2】:

    检查我的笔here。我使用 jquery 根据输入选择定义了 href 的值。

    你将使用 attr 方法:

    $('a#attibute').attr("href", "the url comes here");
    

    希望有效果

    【讨论】:

    • 值得注意的是 OP 想要 php 解决方案 - jquery 可能在他们的网站上不可用
    • 感谢您的快速答复!不幸的是,我需要使用 php。使用 jQuery 会很容易,但这个 str_replace 事情让我很生气。
    猜你喜欢
    • 1970-01-01
    • 2015-03-09
    • 2023-03-11
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多