【问题标题】:How can I break apart a string kind of like bbcode?如何分解类似于 bbcode 的字符串?
【发布时间】:2018-03-30 06:10:19
【问题描述】:
$input = "This is some random text. OH! Look at this link [link=http://www.google.com] google google[/link]";

好的,所以我正在制作一个论坛网站,我希望人们能够使用像[link=*link*]*text*[/link] 这样的标签,我想把它变成<a href="*link*>*text*</a>

我需要它来分解字符串以将其转换为 html 的输出。我已经尝试了很多东西,但这对我来说有点困惑。任何帮助都会很棒!

【问题讨论】:

  • I've tried a lot of things-> 我看不到。在您的问题中发表您的努力,让人们相信您确实尝试过。

标签: php html forum


【解决方案1】:

使用preg_replace函数

示例代码:

$find = array('~\[link=(.*?)\](.*?)\[/link\]~s');
$replace = array('<a href="$1">$2</a>');

$replaced = preg_replace($find, $replace, $text);

当然你可以为数组 $find 和 $replace 添加更多替换

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    相关资源
    最近更新 更多