【发布时间】:2014-10-23 16:07:03
【问题描述】:
我做了这个函数来查找文本字符串中的特定字符集并将它们转换为 html 标签:
function ccfc($content)
{
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// $code_block = preg_replace($reg_exUrl, "<a href=".$url[0].">{$url[0]}</a> ", $content);
if(preg_match($reg_exUrl, $content, $url)) {
// make the urls hyper links
$content = preg_replace($reg_exUrl, "<a href=".$url[0].">{$url[0]}</a> ", $content);
} else {
// if no urls in the text just return the text
$content = $content;
}
$code_block = preg_replace_callback(
'/([\`]{3})(.*?)([\`]{3})/s',
function($matches) {
$matches[2] = htmlentities($matches[2]);
return '<pre><code>'. $matches[2] .'</code></pre>';
},
$content);
$bold = preg_replace_callback(
'/([\*]{2})(.*?)([\*]{2})/s',
function($matches) {
$matches[2] = htmlentities($matches[2]);
return '<b>'. $matches[2] .'</b>';
},
$code_block);
$italic = preg_replace_callback(
'/([\*]{1})(.*?)([\*]{1})/s',
function($matches) {
$matches[2] = htmlentities($matches[2]);
return '<i>'. $matches[2] .'</i>';
},
$bold);
return $italic;
}
这个函数会找到像http://www.google.com这样的url并将它们转换为链接
第二个会找到```代码内容```并转换成<pre><code> code content </code></pre>
第三个会找到**内容**并转换为<b> content </b>
第四个会找到*内容*并将其转换为<i> content </i>
但是如果代码写在 ``` ``` 之外,它就会被执行。如何让剩余的文本使用 htmlentities()?
【问题讨论】:
-
<b>和<i>标签已弃用;您应该将文本放在<span>或其他标签中,并设置一个 css 类。 -
@ialarmedalien 或者使用
<strong>和<em>如果OP 试图传达强调,这似乎是他试图用降价转换来做