我有此功能来识别和转换标签,表情符号等
function convert_text($str) {
$regex = "/[@#](\w+)/";
//type and links
$hrefs = [
'#' => 'hashtag.php?hashtag',
'@' => 'user.php?user'
];
$result = preg_replace_callback($regex, function($matches) use ($hrefs) {
return sprintf(
'<a href="%s=%s">%s</a>',
$hrefs[$matches[0][0]],
$matches[1],
$matches[0]
);
}, $str);
//$result = preg_replace("/U\+([A-F0-9]{5})/", '\u{${1}}', $result);
$result = preg_replace('/U\+([A-F0-9]{5})/', '<span style="font-size:30px;">&#x\\1;</span>', $result);
return ($result);
}
我想让它从文本中识别http://
和https://
,而不是转换为:
<a href="http://link.com">http://link.com</a>
如何在函数内部实现呢?
参考方案
对于构造一个世界征服者的正则表达式模式,以提取世界上所有可以梦想的有效URL(包括unicode),同时拒绝具有有效字符但不合逻辑结构的URL,我不会费心。 (我将与Gumbo一起继续。)
有关正则表达式的演示,请参见:https://regex101.com/r/HFCP1Z/1/
注意事项:
如果URL匹配,则没有捕获组,因此不会生成$m[1]
。如果匹配了用户/哈希标签,则会生成全字符串匹配和捕获组1。如果匹配了表情符号,则填充全字符串匹配项,捕获组1元素为空(但由于php生成$m
作为索引数组-没有间隔)而被声明,捕获组2保留了表情符号的括号子字符串。
您需要确保不要意外替换包含合格的标签/用户标签子字符串的一部分URL。 (当前,其他答案不考虑此漏洞。)我将通过在输入上执行一次传递并在其他模式没有机会使用之前消耗整个URL子字符串来防止这种情况发生。(注意:http://example.com/@dave
和http://example.com?asdf=1234#anchor
)
我将您的标签/用户标签查找数组声明为常量有两个原因。
它没有变化,因此不必是变量。
它具有全局作用域,因此use()
内不需要preg_replace_callback()
语法。
您应该避免在代码中添加内联样式。我建议分配一个类,以便您以后决定修改/扩展样式时可以只更新样式表的单个部分。
代码:(Demo)
define('PINGTAGS', [
'#' => 'hashtag.php?hashtag',
'@' => 'user.php?user'
]);
function convert_text($str) {
return preg_replace_callback(
"~(?i)\bhttps?[-\w.\~:/?#[\]@!$&'()*+,;=]+|[@#](\w+)|U\+([A-F\d]{5})~",
function($m) {
// var_export($m); // see for yourself
if (!isset($m[1])) { // url
return sprintf('<a href="%s">%s</a>', $m[0], $m[0]);
}
if (!isset($m[2])) { // pingtag
return sprintf('<a href="%s=%s">%s</a>', PINGTAGS[$m[0][0]], $m[1], $m[0]);
}
return "<span class=\"emoji\">&#x{$m[2]};</span>"; // emoji
},
$str);
}
echo convert_text(
<<<STRING
This is a @ping and a #hash.
This is a www.example.com, this is http://example.com?asdf=1234#anchor
https://www.example.net/a/b/c/?g=5&awesome=foobar# U+23232 http://www5.example.com
https://sub.sub.www.example.org/ @pong@pug#tagged
http://example.com/@dave
more http://example.com/more_(than)_one_(parens)
andU+98765more http://example.com/blah_(wikipedia)#cite-1
and more http://example.com/blah_(wikipedia)_blah#cite-1
and more http://example.com/(something)?after=parens
STRING
);
原始输出:
This is a <a href="user.php?user=ping">@ping</a> and a <a href="hashtag.php?hashtag=hash">#hash</a>.
This is a www.example.com, this is <a href="http://example.com?asdf=1234#anchor">http://example.com?asdf=1234#anchor</a>
<a href="https://www.example.net/a/b/c/?g=5&awesome=foobar#">https://www.example.net/a/b/c/?g=5&awesome=foobar#</a> <span class="emoji">𣈲</span> <a href="http://www5.example.com">http://www5.example.com</a>
<a href="https://sub.sub.www.example.org/">https://sub.sub.www.example.org/</a> <a href="user.php?user=pong">@pong</a><a href="user.php?user=pug">@pug</a><a href="hashtag.php?hashtag=tagged">#tagged</a>
<a href="http://example.com/@dave">http://example.com/@dave</a>
more <a href="http://example.com/more_(than)_one_(parens)">http://example.com/more_(than)_one_(parens)</a>
and<span class="emoji"></span>more <a href="http://example.com/blah_(wikipedia)#cite-1">http://example.com/blah_(wikipedia)#cite-1</a>
and more <a href="http://example.com/blah_(wikipedia)_blah#cite-1">http://example.com/blah_(wikipedia)_blah#cite-1</a>
and more <a href="http://example.com/(something)?after=parens">http://example.com/(something)?after=parens</a>
Stackoverflow呈现的输出:
这是@ping和#hash。
这是www.example.com,这是 http://example.com?asdf=1234#anchor
https://www.example.net/a/b/c/?g=5&awesome=foobar#? http://www5.example.com
https://sub.sub.www.example.org/ @ pong @ pug#标记
http://example.com/@dave
更多 http://example.com/more_(than)one(parens)
和?更多 http://example.com/blah_(wikipedia)#cite-1
以及更多 http://example.com/blah_(wikipedia)_blah#cite-1
以及更多 http://example.com/(something)?after=parens
ps。 hash和user标签在这里没有突出显示,但是它们是您要求的本地链接。
jQuery不起作用 - php我正在使用带有ajax的jquery。有时,给出错误$未定义。这是我的代码:<script language="javascript" type="text/javascript"> var base_path="<? echo $this->baseUrl().'/…
Div单击与单选按钮相同吗? - php有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date…
PHP-复选框组 - php我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…
PHP:正则表达式匹配短语不包含特定单词 - php我已经厌倦了寻找足够接近的例子,花时间寻求一些快速帮助!这是我的代码:preg_match_all( '#<li.*?>.*?</li>#s', $card_html, $activity ); 我想对其进行修改,以使<li.*?>排除单词Unplayed。 (单词出现在.*?之后和>之前。编辑:…
jQuery如果选中则启用动态输入文本 - php我正在使用PHP生成具有表格的表格,例如:<table class="table table-hover"> <thead> <tr> <th class="table-checkbox"> </th> <th> Description </…