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


remove trailing slash from hyperlink href

这与我以前的帖子几乎相同,但是一个不同的问题。

我有一个变量 $this->post['message'] 这是用户发布的任何内容。

如果用户发帖:

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/$2/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']);

这会在帖子中找到藤蔓链接并将它们转换为 iframe。 不过,我遇到了结束斜杠的问题。

如果用户发布https://vine.co/v/iF20jKHvnqg提交后变成<a href="https://vine.co/v/iF20jKHvnqg" target="_blank">https://vine.co/v/iF20jKHvnqg</a>则转换为 iframe 罚款。

但是,如果用户发布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/$2/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']内部的藤蔓链接并删除尾部斜杠,甚至从$this->post['message']内的任何链接中删除尾部斜杠

所以如果

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

它会变成

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

我目前主要关注 Vine 链接,但如果有可能对所有链接做,从长远来看可能会更好。

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

$drc_embed_vine =  '<iframe src="https://vine.co/v/$2/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;

所以帖子是

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

而且rtrim没有效果=/

但如果我改变

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

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

每个帖子都转向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/$2/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);

工作更新!

理查兹回答后,我玩弄了一下插件,想出了

$drc_embed_vine =  '<iframe src="https://vine.co/v/$2/embed/simple" width="480" height="480" frameborder="0"></iframe>';
$this->post['message'] = preg_replace('+/(["<])+', '$1', $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']);

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

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

但是我试图得到一个尽可能简单的例子来模拟你试图解决的问题。

// 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('+/(["<])+', '$1', $after);
// output
echo "trim_user $trim_user'n";
echo "trim_after $trim_after'n";

因此,您可以拥有 2 个输入 - 来自 $user 中的用户的原始消息或$after中带有 HTML 链接的替换文本。

然后在$user上,我只是在测试 rtrim,它有效。

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

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

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

(( 括号表示一个组,将根据组的顺序存储在 $1、$2、$3 中。在这种情况下,只有 1 组,所以我将其称为 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>

根据示例编辑

您可以避免在 (( 中捕获/by [^/]* 而不是 .*。

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/$2/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();

试试这个

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