PHP-解析URL';在忽略所有HTML标记的情况下,在消息中添加
我试图在一个小型的、私有的票务系统中处理消息,该系统将自动将URL解析为可点击的链接,而不会弄乱任何可能发布的HTML。到目前为止,解析URL的功能运行良好,但是系统的一两个用户希望能够发布嵌入的图像,而不是作为附件 这是将字符串转换为可点击URL的现有代码,请注意,我对正则表达式的了解有限,并且依赖于其他人的帮助来构建它PHP-解析URL';在忽略所有HTML标记的情况下,在消息中添加,php,regex,Php,Regex,我试图在一个小型的、私有的票务系统中处理消息,该系统将自动将URL解析为可点击的链接,而不会弄乱任何可能发布的HTML。到目前为止,解析URL的功能运行良好,但是系统的一两个用户希望能够发布嵌入的图像,而不是作为附件 这是将字符串转换为可点击URL的现有代码,请注意,我对正则表达式的了解有限,并且依赖于其他人的帮助来构建它 $text=preg\u replace( 排列( “/(^ |\s |>)(www.[^\n\r]+)/iex”, “/(^ A-Za-z0-9-])+(\\.[A-Za-
$text=preg\u replace(
排列(
“/(^ |\s |>)(www.[^\n\r]+)/iex”,
“/(^ A-Za-z0-9-])+(\\.[A-Za-z]{2,3})\\.[A-Za-z]{2,4}\\/[^\n\r]+)/iex”,
'/(?(?= \\3':'\\0'))",
“带斜杠((strlen('\\2')>0?'\\1\\4':'\\0')”,
“带斜杠((strlen('\\2')>0?'\\1':'\\0')”,
)美元文本);
返回$text;
我如何修改一个现有的函数,比如上面的函数,以排除用HTML标记包装的点击,比如`
变成
`<img src="https://example.com/image.jpg">`
``
我在发帖前做了一些搜索,我找到的最受欢迎的点击是
img
标签和URL。其他所有内容都是简单的,没有列表、代码标签和引号等
我非常感谢你在这里的帮助
摘要:
如何修改现有的一组正则表达式规则以排除img中的匹配项或文本块中的其他html标记。从
\e
修饰符错误中收集到的信息来看,您的php版本最多只能是PHP5.4。
preg\u replace\u callback()
可从PHP5.4或更高版本获得,因此它可能会很紧张
虽然我不想被牵扯进一个有大量答案编辑的大来回,但我想给你一些牵引力
我遵循的方法肯定不是我职业生涯的赌注。正如在这个问题下的评论中以及在很多关于SO的页面中所说的,HTML不应该被正则表达式解析。(完整的免责声明)
&
$text=”这有一个img标记,应该对其进行IGRNORD。
这是一个需要成为标签的img:https://example.com/image.jpg.
这是一个目标。
这是没有目标的。
这是一个未标记的urlhttp://example.com/image.jpg.
(请扩展此测试用例组,以隔离任何Monkey扳手用例)
另一个简短的url示例:example.com/
另一个简短的url示例是.com/index.php?a=b&c=d
另一个www.example.com';
$pattern='~]+?>(*跳过)(*失败)|(((?:https?:)(?:/{2})(w{3})?\S+(\.\S+)+\b(?:[?\&/]\S*)*)~;
塔吉菲函数(百万美元){
如果(preg_match(“/^bmp | gif | png | je?pg/”,$m[4]){//根据需要添加更多文件类型
返回“”;
}否则{
//ValuEx销(PARSEURURL($M[0));/ /如果需要做准备,请考虑使用PARSEGURURL()
返回“”;
}
}
$text=preg_replace_回调($pattern,'taggify',$text);
echo$文本;
输出:
$text='This has an img tag <img src="https://example.com/image.jpg"> that should be igrnored.
This is an img that needs to become a tag: https://example.com/image.jpg.
This is a <a href="https://www.example.com/image" target="_blank">tagged link</a> with target.
This is a <a href="https://example.com/image?what=something&when=something">tagged link</a> without target.
This is an untagged url http://example.com/image.jpg.
(Please extend this battery of test cases to isolate any monkeywrenching cases)
Another short url example.com/
Another short url example.com/index.php?a=b&c=d
Another www.example.com';
$pattern='~<(?:a|img)[^>]+?>(*SKIP)(*FAIL)|(((?:https?:)?(?:/{2})?)(w{3})?\S+(\.\S+)+\b(?:[?#&/]\S*)*)~';
function taggify($m){
if(preg_match('/^bmp|gif|png|je?pg/',$m[4])){ // add more filetypes as needed
return "<img src=\"{$m[0]}\">";
}else{
//var_export(parse_url($m[0])); // if you need to do preparations, consider using parse_url()
return "<a href=\"{$m[0]}\" target=\"_blank\">{$m[0]}</a>";
}
}
$text=preg_replace_callback($pattern,'taggify',$text);
echo $text;
这有一个img标签,应该对其进行IGRNORD。
这是一个需要成为标记的img:。
这是一个目标。
这是没有目标的。
这是一个未标记的url。
(请扩展此测试用例组,以隔离任何Monkey扳手用例)
另一个简短的url
另一个简短的url
另一个
SKIP-FAIL技术可以“取消”不需要的匹配。符合条件的匹配将由管道后面的模式部分(
|
)在(*SKIP)(*FAIL)
之后表示,如果我们要打破好的建议。。。我们是否可以看到一些可能导致问题的示例输入?让我们看看一些输入数据和一些预期的输出。用示例修改有效点。为什么不使用库呢@LawrenceCherone OP希望流程“轻松”,避免使用图书馆和其他此类可靠的东西。轻松!=对……谢谢你,这也让我更好地了解了它的工作原理=)
`<img src="<a href="https://example.com/image.jpg" target="_blank">example.com/image.jpg</a>">`
$text='This has an img tag <img src="https://example.com/image.jpg"> that should be igrnored.
This is an img that needs to become a tag: https://example.com/image.jpg.
This is a <a href="https://www.example.com/image" target="_blank">tagged link</a> with target.
This is a <a href="https://example.com/image?what=something&when=something">tagged link</a> without target.
This is an untagged url http://example.com/image.jpg.
(Please extend this battery of test cases to isolate any monkeywrenching cases)
Another short url example.com/
Another short url example.com/index.php?a=b&c=d
Another www.example.com';
$pattern='~<(?:a|img)[^>]+?>(*SKIP)(*FAIL)|(((?:https?:)?(?:/{2})?)(w{3})?\S+(\.\S+)+\b(?:[?#&/]\S*)*)~';
function taggify($m){
if(preg_match('/^bmp|gif|png|je?pg/',$m[4])){ // add more filetypes as needed
return "<img src=\"{$m[0]}\">";
}else{
//var_export(parse_url($m[0])); // if you need to do preparations, consider using parse_url()
return "<a href=\"{$m[0]}\" target=\"_blank\">{$m[0]}</a>";
}
}
$text=preg_replace_callback($pattern,'taggify',$text);
echo $text;
This has an img tag <img src="https://example.com/image.jpg"> that should be igrnored.
This is an img that needs to become a tag: <img src="https://example.com/image.jpg">.
This is a <a href="https://www.example.com/image" target="_blank">tagged link</a> with target.
This is a <a href="https://example.com/image?what=something&when=something">tagged link</a> without target.
This is an untagged url <img src="http://example.com/image.jpg">.
(Please extend this battery of test cases to isolate any monkeywrenching cases)
Another short url <a href="example.com/" target="_blank">example.com/</a>
Another short url <a href="example.com/index.php?a=b&c=d" target="_blank">example.com/index.php?a=b&c=d</a>
Another <a href="www.example.com" target="_blank">www.example.com</a>