Php 使用preg_replace避免重复替换

Php 使用preg_replace避免重复替换,php,regex,Php,Regex,这是我的php函数。 它会替换已经被替换的东西,从而弄乱HTML。 如何防止在第一次替换相同文本时再次替换 function text2link($str){ $str="\r\n$str\r\n"; $pattern= array( '/(http:\/\/)(.*?)(\n|\<|"|\s)/is', '/(https:\/\/)(.*?)(\n|\<|"|\s)/is', '/\[url\=(.*?)\](.*?)\[\/url\]/is' //'/[^\"|\>

这是我的php函数。 它会替换已经被替换的东西,从而弄乱HTML。 如何防止在第一次替换相同文本时再次替换

function text2link($str){
$str="\r\n$str\r\n";
$pattern= array(  
'/(http:\/\/)(.*?)(\n|\<|"|\s)/is', 
'/(https:\/\/)(.*?)(\n|\<|"|\s)/is', 
'/\[url\=(.*?)\](.*?)\[\/url\]/is' 
//'/[^\"|\>](http:\/\/)([a-zA-Z0-9\?\&\%\.\;\:\/\=\+\_\-]*)[^\"|\<]/is', 
//'/[^\"|\>](https:\/\/)([a-zA-Z0-9\?\&\%\.\;\:\/\=\+\_\-]*)[^\"|\<]/is', 
//'/[^\"|\>](ftp:\/\/)([a-zA-Z0-9\?\&\%\.\;\:\/\=\+\_\-]*)[^\"|\<]/is' 
 );
$replace=array(  ' <a target="_blank" href="http://$2">$2</a> $3', ' <a target="_blank" href="https://$2">$2</a> $3', '<a target="_blank" href="$1">$2</a>', 
//' <a target="_blank" href="http://$2">$2</a> ', ' <a target="_blank" href="https://$2">$2</a> ', ' <a target="_blank" href="ftp://$2">ftp: $2</a> '
);
$str = preg_replace( $pattern, $replace, $str);
return $str;
}


echo text2link(' A link to [url=https://www.google.com] secure google [/url] and www.google.com this is http://www.google.com and another [url=http://www.google.com] google [/url]  '); exit;
函数text2link($str){
$str=“\r\n$str\r\n”;
$pattern=数组(

“/(http:\/\/)(.*)(\n\我希望这就是您要寻找的。这里我们使用
preg\u match\u all
收集所有要替换的匹配项,然后逐个替换

正则表达式:
\[([a-z]+)\=((?:https?:\/\/)?(?:www\)?[^\]+)\](.*?\[\/\\1\]

1.
[([a-z]+)\=
这将匹配
[
,然后匹配一些字符
a-z
,然后
=

2.
((?:https?:\/\/)(?:www\.)?[^\]+)\]
这将捕获完整的链接,然后
]

3.
(.**?\[
这将匹配所有代码,直到
[

4.
\/\\1\]
这将匹配
\
,然后第一个捕获的组像这里一样,第一个捕获的组是
url
,然后在最后是
]

函数text2link($str)
{
preg\u match\u all(“/\[([a-z]+)\=((?:https?:\/\/)?(?:www\)?[^\]+)\](.*?\[\/\\1\]/,$str,$matches);
foreach($0]匹配为$key=>$toReplace)
{
$str=str_替换($toReplace,“,$str);
}
返回$str;
}
echo text2link('指向[url]的链接=https://www.google.com]安全谷歌[/url]和www.google.com这是http://www.google.com 和另一个[url]=http://www.google.com]谷歌[/url]”);
输出:


和www.google.com的链接是http://www.google.com 还有一个

如果我理解正确,您只希望用html


如果要在缺少时添加
www.

代码:

$str='Anhttps://www 链接到[网址]=https://www.google.com]安全谷歌[/url]和www.google.com这是http://www.google.com 和另一个[url]=http://www.google.com]谷歌[/url]这只是www.[url=www.google.com]google[/url],而这个url没有www.[url=google.com]google[/url];
$pattern=“/(\[url=(https?:\/\/)?(www.)([^]+)\]?(.*??\[\/url\])/i”;
$replace='';
echo preg_replace($pattern,$replace,$str);
输出:

 A link to <a target="_blank" href="https://www.google.com">secure google</a> and www.google.com this is http://www.google.com and another <a target="_blank" href="http://www.google.com">google</a>
An https://www link to <a target=\"_blank\" href=\"https://www.google.com\">secure google</a> and www.google.com this is http://www.google.com and another <a target=\"_blank\" href=\"http://www.google.com\">google</a> and this is just www. <a target=\"_blank\" href=\"www.google.com\">google</a> and this url has no www. <a target=\"_blank\" href=\"www.google.com\">google</a>
Anhttps://www 链接到www.google.com这是http://www.google.com 还有一个,这只是www,这个网址没有www。

@mickmackusa感谢您的代码!对其进行了一点扩展,下面的代码适用于字符串中各种类型的URL等。谢谢:) 在这里发布,以便其他人也可以使用它

$str=' A link to [url=https://www.google.com] secure google [/url] and www.google.com this is http://www.google.com and another [url=http://www.google.com] google [/url]  ';

$pattern=array(
'/(\[url=(https?[^]]+)\] ?(.*?) ?\[\/url\])/is',
 '/([\\s|\\n])(http:\/\/)(.*?)([\\n|\<|\"|\\s])/is',
 '/([\\s|\\n])(https:\/\/)(.*?)([\\n|\<|\"|\\s])/is',
'/([\\s|\\n])(www\.)(.*?)([\\n|\<|\"|\\s])/is'
);
$replace=array(
' <a target="_blank" href="$2">$3</a> ',
' <a target="_blank" href="http://$3">$3</a> ',
' <a target="_blank" href="https://$3">$3</a> ',
' <a target="_blank" href="http://www.$3">$3</a> '
);
echo preg_replace($pattern,$replace,$str);
$str='指向[url]的链接=https://www.google.com]安全谷歌[/url]和www.google.com这是http://www.google.com 和另一个[url]=http://www.google.com]谷歌[/url]';
$pattern=数组(
“/(\[url=(https?[^]+)\]?(.*???\[\/url\])/is',

“/([\\s\\\\n])(http:\/\/)(.*)([\\n\\\还有,如何为以www而不是http开头的文本添加模式?如果您使用输入字符串和预期输出更新您的帖子,那就更好了。或者,
”/\[url=([^\]+)\](.+?)(\[\[\/url\])/“,”
@SahilGulati您可能想阅读:@SumitKumar这看起来像MyBB标记结构,但可能是许多其他软件的语法。您的项目使用什么来创建方括号URL?如果您在问题(甚至标题)中包含该名称,它可能会帮助人们找到您的问题。这不是我要替换的字符串。这是我在原始字符串上运行preg时得到的输出。原始字符串在这一行echo text2link()中。此外,我不能使用str_replace,因为字符串来自用户。它不会一直是google…@SumitKumar对不起,我误解了你的问题,我希望这篇文章能帮助你知道我已经更新了它。这不是一个扩展场景。第一段代码中的问题是当“[URL]“模式替换了http/https,然后其他模式也“替换”了URL模式中已替换的http和https。我在此处发布的答案解决了“重复替换”问题,即此问题的主题。如果您仍然认为我应该编辑此问题,请告诉我。我会编辑它。谢谢:)
$str=' A link to [url=https://www.google.com] secure google [/url] and www.google.com this is http://www.google.com and another [url=http://www.google.com] google [/url]  ';
$pattern="/(\[url=(https?[^]]+)\] ?(.*?) ?\[\/url\])/i";
$replace="<a target=\"_blank\" href=\"$2\">$3</a>";
echo preg_replace($pattern,$replace,$str);  // I recommend trim() around preg_replace() here
 A link to <a target="_blank" href="https://www.google.com">secure google</a> and www.google.com this is http://www.google.com and another <a target="_blank" href="http://www.google.com">google</a>
(\[url=((?:https?:\/\/)?[^]]+)\] ?(.*?) ?\[\/url\])
$str='An https://www link to [url=https://www.google.com] secure google [/url] and www.google.com this is http://www.google.com and another [url=http://www.google.com] google [/url] and this is just www. [url=www.google.com] google [/url] and this url has no www. [url=google.com] google [/url]';
$pattern="/(\[url=(https?:\/\/)?(www.)?([^]]+)\] ?(.*?) ?\[\/url\])/i";
$replace='<a target=\"_blank\" href=\"$2www.$4\">$5</a>';
echo preg_replace($pattern,$replace,$str);
An https://www link to <a target=\"_blank\" href=\"https://www.google.com\">secure google</a> and www.google.com this is http://www.google.com and another <a target=\"_blank\" href=\"http://www.google.com\">google</a> and this is just www. <a target=\"_blank\" href=\"www.google.com\">google</a> and this url has no www. <a target=\"_blank\" href=\"www.google.com\">google</a>
$str=' A link to [url=https://www.google.com] secure google [/url] and www.google.com this is http://www.google.com and another [url=http://www.google.com] google [/url]  ';

$pattern=array(
'/(\[url=(https?[^]]+)\] ?(.*?) ?\[\/url\])/is',
 '/([\\s|\\n])(http:\/\/)(.*?)([\\n|\<|\"|\\s])/is',
 '/([\\s|\\n])(https:\/\/)(.*?)([\\n|\<|\"|\\s])/is',
'/([\\s|\\n])(www\.)(.*?)([\\n|\<|\"|\\s])/is'
);
$replace=array(
' <a target="_blank" href="$2">$3</a> ',
' <a target="_blank" href="http://$3">$3</a> ',
' <a target="_blank" href="https://$3">$3</a> ',
' <a target="_blank" href="http://www.$3">$3</a> '
);
echo preg_replace($pattern,$replace,$str);