字符串转 HTML?

发布于 2024-12-09 19:24:53 字数 1096 浏览 0 评论 0原文

我不明白为什么下面的代码将字符串返回为字符串而不是html,

function en_code($string)
{
    # find the match of [br] = a break = <br>
    $output = preg_replace('/\[(?:&nbsp;|\s)*([br]+)(?:&nbsp;|\s)*\]/', '&lt;$1 /&gt;', $string);

    # return the result
    return $output;
}

$string = 'Wallace and Gromit\'s Children Foundation the whole campaign was exemplary, showing true professionalism, creativity and an amazing understanding of what makes a strong news story.\'[br][br]Wallace & Gromit\'s Children\'s Foundation';

echo en_code($string);

返回,

 Wallace and Gromit's Children Foundation the whole campaign was
 exemplary, showing true professionalism, creativity and an amazing
 understanding of what makes a strong news story.'<br /><br />Wallace &
 Gromit's Children's Foundation

但它应该像这样返回,

超级无敌掌门狗儿童基金会整个活动都是 堪称典范,展现出真正的专业精神、创造力和令人惊叹的 了解如何打造强有力的新闻报道。'

华莱士和华莱士 格罗米特儿童基金会

我想做的是将函数中的 [br] 转换为

有什么想法吗?

I can't understand why the code below return the string as a string but not as html,

function en_code($string)
{
    # find the match of [br] = a break = <br>
    $output = preg_replace('/\[(?: |\s)*([br]+)(?: |\s)*\]/', '<$1 />', $string);

    # return the result
    return $output;
}

$string = 'Wallace and Gromit\'s Children Foundation the whole campaign was exemplary, showing true professionalism, creativity and an amazing understanding of what makes a strong news story.\'[br][br]Wallace & Gromit\'s Children\'s Foundation';

echo en_code($string);

returns,

 Wallace and Gromit's Children Foundation the whole campaign was
 exemplary, showing true professionalism, creativity and an amazing
 understanding of what makes a strong news story.'<br /><br />Wallace &
 Gromit's Children's Foundation

but it should return like this,

Wallace and Gromit's Children Foundation the whole campaign was
exemplary, showing true professionalism, creativity and an amazing
understanding of what makes a strong news story.'

Wallace &
Gromit's Children's Foundation

What I am trying to do is to convert [br] to <br /> in the function.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-12-16 19:24:53

尝试更改

$output = preg_replace('/\[(?: |\s)*([br]+)(?: |\s)*\]/', '<$1 />', $string);

$output = preg_replace('/\[(?: |\s)*([br]+)(?: |\s)*\]/', '<$1 />', $string);

替换使用 <> 的 HTML 代码而不是实际字符。

Try changing

$output = preg_replace('/\[(?: |\s)*([br]+)(?: |\s)*\]/', '<$1 />', $string);

to

$output = preg_replace('/\[(?: |\s)*([br]+)(?: |\s)*\]/', '<$1 />', $string);

The replacement was using the HTML code for < and > rather than the actual characters.

暖风昔人 2024-12-16 19:24:53

您正在使用 <> ,它们是 HTML 实体,用于显示 <> 。 html 页面上的 符号。您需要专门使用这些字符而不是实体,以便结果具有有效的 html 标签。

You're using < and > which are HTML entities use to display the < and > symbols on html pages. You need to use specifically these chars intead of the entities in order for the result to have valid html tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文