从超链接 href 中删除尾部斜杠

remove trailing slash from hyperlink href

这与我之前 post 的信息几乎相同,但问题不同。

我有一个 $this->post['message'] 的变量,它是用户 post 的任何值。

如果用户 posts:

Check out this vine https://vine.co/v/iF20jKHvnqg/

一旦提交,html 输出如下:

Check out this vine <a href="https://vine.co/v/iF20jKHvnqg/" target="_blank">https://vine.co/v/iF20jKHvnqg/</a>

这就是 $this->post['message'] 等于。

所以在我的后台我创建了一个插件

$drc_embed_vine =  '<iframe src="https://vine.co/v//embed/simple" width="480" height="480" frameborder="0"></iframe>';

$this->post['message'] = preg_replace('~(<a href="https?://vine.co)/v/(.*)" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $this->post['message']);

这会在 posts 中找到 vine links 并将它们转换为 iframe。不过,我遇到了结尾斜杠的问题。

如果用户 posts https://vine.co/v/iF20jKHvnqg 一旦提交变成 <a href="https://vine.co/v/iF20jKHvnqg" target="_blank">https://vine.co/v/iF20jKHvnqg</a> 它会转换为 iframe 罚款。

但是如果用户 posts https://vine.co/v/iF20jKHvnqg/ 更改为 <a href="https://vine.co/v/iF20jKHvnqg/" target="_blank">https://vine.co/v/iF20jKHvnqg/</a> 它不会被转换。不同之处在于尾部斜杠。

现在我试过了:

$this->post['message'] = rtrim($this->post['message'],"/");

$drc_embed_vine =  '<iframe src="https://vine.co/v//embed/simple" width="480" height="480" frameborder="0"></iframe>';

$this->post['message'] = preg_replace('~(<a href="https?://vine.co)/v/(.*)" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $this->post['message']);

但这似乎行不通,有没有办法我可以在 $this->post['message'] 内定位 vine links 并删除尾部斜杠,或者甚至删除任何 link里面$this->post['message']

所以如果

$this->post['message'] = 'Go to <a href="http://whosebug.com/questions/ask/" target="_blank">http://whosebug.com/questions/ask/</a>';

会变成

$this->post['message'] = 'Go to <a href="http://whosebug.com/questions/ask" target="_blank">http://whosebug.com/questions/ask</a>';

目前我主要关注 Vine links,但如果可以对所有 links 做,那么长期 运行 可能会更好.

我最近一次失败的尝试(我仍在努力解决这个问题)

$drc_embed_vine =  '<iframe src="https://vine.co/v//embed/simple" width="480" height="480" frameborder="0"></iframe>';
$str = $this->post['message'];
$str = rtrim($str, '/');
$str = preg_replace('~(<a href="https?://vine.co)/v/(.*)" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $str);

尝试了不同的东西 只是胡闹,我试了一下

$str = $this->post['message'];
$str = rtrim($str, '/');

$this->post['message'] = $str;

所以post是

<a href="http://vine.co/v/iF20jKHvnqg/" target="_blank">http://vine.co/v/iF20jKHvnqg/</a>

rtrim 没有效果 =/

但如果我改变

$this->post['message'] = $str;

$this->post['message'] = test;

每个 post 都变成 TEST 所以我不明白为什么 rtrim 没有效果 =/

为清楚起见更新

$this->post['message'] = 'Check out this vine <a href="http://vine.co/v/iF20jKHvnqg/" target="_blank">http://vine.co/v/iF20jKHvnqg/</a>';
$drc_embed_vine =  '<iframe src="https://vine.co/v//embed/simple" width="480" height="480" frameborder="0"></iframe>';
$str = $this->post['message'];
$str = rtrim($str, '/');
$str = preg_replace('~(<a href="https?://vine.co)/v/(.*)" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $str);

正在进行更新!

在 Richards 回答后,我对插件进行了一些尝试并想出了

$drc_embed_vine =  '<iframe src="https://vine.co/v//embed/simple" width="480" height="480" frameborder="0"></iframe>';
$this->post['message'] = preg_replace('+/(["<])+', '', $this->post['message']);
$this->post['message'] = preg_replace('~(<a href="https?://vine.co)/v/(.*)" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $this->post['message']);

这行得通! =) 但这是最​​有效的方法吗...?

试试这个

$str = $this->post['message'];
$str = rtrim($str, '/');

请参阅下面的示例。我认为您的问题过于复杂,我没有看到它正在处理替换 $post->['message'] 或为 iframe 生成 link 的代码。

但我已尝试获取一个尽可能简单的示例来模拟您要解决的问题

// input
$user = 'Check out this vine https://vine.co/v/iF20jKHvnqg/';
$after = 'Check out this vine <a href="https://vine.co/v/iF20jKHvnqg/" target="_blank">https://vine.co/v/iF20jKHvnqg/</a>';

// todo
$trim_user = rtrim($user, '/');
$trim_after = preg_replace('+/(["<])+', '', $after);

// output
echo "trim_user $trim_user\n";
echo "trim_after $trim_after\n";

所以您可以有 2 个输入 - $user 中来自用户的原始消息或 $after 中用 HTML links 替换的文本。

然后在 $user 上,我正在测试 rtrim,它可以工作。

在 $after 上,我需要替换所有出现在 " 或 < 之前的 /。这是因为引号 "" 在和结束标记中。我用一个正则表达式替换两者。

查看参数。第一个是匹配模式的正则表达式,第二个是匹配模式的替换,我跳过斜杠 / 并只使用我需要保留的第二部分。

[] 正则表达式中的方括号表示字符组 - 如 [abc] 是 a、b 或 c 的任何字符。如果有 - 这是一个范围。就像 [a-z] 是介于 a 和 z 之间的任何值。

()中括号表示一个组,按照组的先后顺序分别存放在$1,$2,$3中。在这种情况下,只有 1 个组,所以我将其称为 $1。

换句话说,/ 和 " 和 < 中的任何字符都将被 "clipboard" $1 替换。这又是 " 或 <.

最后一件事,我正在将输出写入控制台。

trim_user Check out this vine https://vine.co/v/iF20jKHvnqg
trim_after Check out this vine <a href="https://vine.co/v/iF20jKHvnqg" target="_blank">https://vine.co/v/iF20jKHvnqg</a>

根据示例编辑

您可以通过 [^/]* 而不是 () 中的 .* 来避免捕获 /。

class Example
{

    public function run()
    {
        $this->post['message'] = 'Check out this vine <a href="http://vine.co/v/iF20jKHvnqg/" target="_blank">http://vine.co/v/iF20jKHvnqg/</a>';
        $drc_embed_vine = '<iframe src="https://vine.co/v//embed/simple" width="480" height="480" frameborder="0"></iframe>';
        $str = $this->post['message'];
        $str = rtrim($str, '/');
        $str = preg_replace('~(<a href="https?://vine.co)/v/([^/]*)/?" target="_blank">(https?://vine.co)/v/(.*)<\/a>~', $drc_embed_vine, $str);

        echo $str;
    }
}

$example = new Example();
$example->run();