Php 如何使用preg_replace替换链接和图像链接
我有包含链接和图片链接的文本,可以有很多链接并与其他单词混合。 下面的文本是我的示例文本Php 如何使用preg_replace替换链接和图像链接,php,preg-replace,preg-match,preg-match-all,preg-replace-callback,Php,Preg Replace,Preg Match,Preg Match All,Preg Replace Callback,我有包含链接和图片链接的文本,可以有很多链接并与其他单词混合。 下面的文本是我的示例文本 $string = "http://www.google.com/ is best know for search engine, this is Google logo ##https://www.google.com/images/srpr/logo11w.png##. And you can visit http://www.youtube.com/ to watch videos. Here You
$string = "http://www.google.com/ is best know for search engine, this is Google logo ##https://www.google.com/images/srpr/logo11w.png##. And you can visit http://www.youtube.com/ to watch videos. Here YouTube's logo ##http://s.ytimg.com/yts/img/pixel-vfl3z5WfW.gif##";
我想用preg_ureplace
来替换它们
$string = '<a href="http://www.google.com/">http://www.google.com/</a> is best know for search engine, this is Google logo <img src="https://www.google.com/images/srpr/logo11w.png" />. And you can visit <a href="http://www.youtube.com/">http://www.youtube.com/</a> to watch videos. Here YouTube's logo <img src="http://s.ytimg.com/yts/img/pixel-vfl3z5WfW.gif"></img>';
这是图片的preg\u replace
$string = preg_replace("/\#\#([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])\#\#/i","<img src=\"$1\"></img>",$string);
$string=preg\u replace(“/\\\\\\\\”([\w]+:\/\/[\w-?&;\\~=\.\/\@]+[\w\/])\\\\\\/i,“,$string”);
它们都很好地工作,但它们不会在链接和图像链接之间分离。请帮助我,谢谢我已经尝试了一整天。您正在寻找的可能是
preg\u replace\u callback
,它可以根据匹配结果更改替换字符串:
$str = preg_replace_callback('~(##)?\b((?:f|ht)tps?://\S+)(?(1)##|(?=\s|$|\pP))~',
function ($m) {
if (isset($m[1])) return '<img src="' . $m[2] . '"/>';
return '<a href="' . $m[2] . '">' . $m[2] . '</a>';
},
$str);
$str=preg#u replace|u callback('~(##)?\b((?:f | ht)tps?:/\S+(?=\S$| | | pP))~,
功能(百万美元){
如果(isset($m[1])返回“”;
返回“”;
},
$str);
图案细节:
模式在(?(1).|…)
末尾使用了一个特定的功能,该功能是一个条件(如果捕获组1存在,则尝试此功能,否则尝试此功能)
\pP
是包含所有操作字符的字符类\p{Punct}
的快捷方式。我把它放在交替处理这种字符串中:诸如此类(http://domain.com/file.html)
由于描述URL的模式非常基本(在我看来,尝试创建更复杂的模式来描述URL是浪费时间的),为了确保URL是正确的,您可以在回调函数中使用
filter\u var
进行检查。您要查找的可能是preg\u replace\u callback
,它可以根据匹配结果更改替换字符串:
$str = preg_replace_callback('~(##)?\b((?:f|ht)tps?://\S+)(?(1)##|(?=\s|$|\pP))~',
function ($m) {
if (isset($m[1])) return '<img src="' . $m[2] . '"/>';
return '<a href="' . $m[2] . '">' . $m[2] . '</a>';
},
$str);
$str=preg#u replace|u callback('~(##)?\b((?:f | ht)tps?:/\S+(?=\S$| | | pP))~,
功能(百万美元){
如果(isset($m[1])返回“”;
返回“”;
},
$str);
图案细节:
模式在(?(1).|…)
末尾使用了一个特定的功能,该功能是一个条件(如果捕获组1存在,则尝试此功能,否则尝试此功能)
\pP
是包含所有操作字符的字符类\p{Punct}
的快捷方式。我把它放在交替处理这种字符串中:诸如此类(http://domain.com/file.html)
由于描述URL的模式非常基本(在我看来,尝试创建更复杂的模式来描述URL是浪费时间的),为了确保URL是正确的,您可以在回调函数中使用
filter\u var
进行检查。因为链接之间唯一可靠的区别是散列标记,我认为您需要使用正向查找在正则表达式之间添加另一层唯一性
/(
/\\\\\\\\\\\(https?:\/\/[\w-?\\\&;~=\.\/\@+[\w\/])\\\\\\\\/i
$string = preg_replace("/((?<!##)https?:\/\/[\w-?#&;~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$string);
$string = preg_replace("/\#\#(https?:\/\/[\w-?#&;~=\.\/\@]+[\w\/])\#\#/i","<img src=\"$1\"></img>",$string);
$string=preg_replace(“/(?”,$string);
我对此进行了测试,使用您的示例似乎对我有效。因为链接之间唯一可靠的区别是散列标记,我认为您需要使用正向查找在正则表达式之间添加另一层唯一性
/(
/\\\\\\\\\\\(https?:\/\/[\w-?\\\&;~=\.\/\@+[\w\/])\\\\\\\\/i
$string = preg_replace("/((?<!##)https?:\/\/[\w-?#&;~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</a>",$string);
$string = preg_replace("/\#\#(https?:\/\/[\w-?#&;~=\.\/\@]+[\w\/])\#\#/i","<img src=\"$1\"></img>",$string);
$string=preg_replace(“/(?”,$string);
我对此运行了一个测试,使用您的示例它似乎对我有效。对于
$string=preg\u replace(“/”(?)对于$string=preg\u replace(“/”)?