首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串到HTML?

字符串到HTML?
EN

Stack Overflow用户
提问于 2011-10-13 13:56:23
回答 2查看 141关注 0票数 2

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

代码语言:javascript
复制
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);

返回,

代码语言:javascript
复制
 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]转换为<br />

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2011-10-13 14:01:29

试着改变

代码语言:javascript
复制
$output = preg_replace('/\[(?:&nbsp;|\s)*([br]+)(?:&nbsp;|\s)*\]/', '&lt;$1 /&gt;', $string);

代码语言:javascript
复制
$output = preg_replace('/\[(?:&nbsp;|\s)*([br]+)(?:&nbsp;|\s)*\]/', '<$1 />', $string);

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

票数 3
EN

Stack Overflow用户

发布于 2011-10-13 14:02:48

您使用的是&lt;&gt;,它们是HTML实体用来在html页面上显示<>符号的。为了使结果具有有效的html标记,您需要具体地使用这些实体的字符整数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7755226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档