gpt4 book ai didi

php - Preg_replace BBCode 链接

转载 作者:可可西里 更新时间:2023-11-01 00:49:58 25 4
gpt4 key购买 nike

我有这个 bbcode:

[url=http://www.youtube.com/watch?v=h1bIEK1h150]If I offer you my soul[/url]

例如。我怎样才能把它变成这样:

<a href="http://www.youtube.com/watch?v=h1bIEK1h150" target="_blank">If I offer you my soul</a>

最佳答案

你需要一个正则表达式。考虑到 bbcode 可以有文本 URL 或只有 URL,您将需要两个语句:

$message = preg_replace('@\[url=([^]]*)\]([^[]*)\[/url\]@', '<a href="$1">$2</a>', $message);
$message = preg_replace('@\[url\]([^[]*)\[/url\]@', '<a href="$1">$1</a>', $message);

此外,如果您从 PHPBB 解析 bbcode,它可以有一个唯一的用户标识符:

$uid = '[0-9a-z]{5,}';
$message = preg_replace('@\[url=([^]]*):'. $uid . '\]([^[]*)\[/url:'. $uid . '\]@', '<a href="$1">$2</a>', $message);
$message = preg_replace('@\[url:'. $uid . '\]([^[]*)\[/url:'. $uid . '\]@', '<a href="$1">$1</a>', $message);

关于php - Preg_replace BBCode 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9726369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com