PHP preg_replace - www 或 http://
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6165552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP preg_replace - www or http://
提问by Matthew
Really stuck on what seems to be something simple. I have a chatbox/shoutbox where there may be arbitrary URLs entered. I want to find each individual URL (separated by spaces) and wrap it in tags.
真的坚持在看似简单的事情上。我有一个聊天框/留言框,其中可能输入了任意 URL。我想找到每个单独的 URL(用空格分隔)并将其包装在标签中。
Example: Harry you're a http://google.comwizard!
= Harry you're a $lhttp://google.com$l wizard!
例子:=Harry you're a http://google.comwizard!
Harry you're a $lhttp://google.com$l wizard!
Example: Harry you're a http://www.google.comwizard!
= Harry you're a $lhttp://www.google.com$l wizard!
例子:=Harry you're a http://www.google.comwizard!
Harry you're a $lhttp://www.google.com$l wizard!
Example: Harry you're a www.google.com wizard!
= Harry you're a $lwww.google.com$l wizard!
例子:Harry you're a www.google.com wizard!
=Harry you're a $lwww.google.com$l wizard!
Sorry if this is a daft question; I'm just trying to make something work and I'm no php expert :(
对不起,如果这是一个愚蠢的问题;我只是想让一些东西工作,我不是 php 专家:(
回答by Bryan
There is an interesting article about a URL regular expression. In PHP this would look like:
有一篇关于URL 正则表达式的有趣文章。在 PHP 中,这看起来像:
$pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>???“”‘']))/";
$inp = "Harry you're a http://google.com wizard!";
$text = preg_replace($pattern, "[supertag][/supertag]", $inp);
And of course replace [supertag]
and [/supertag]
with the appropriate values.
当然[supertag]
,[/supertag]
用适当的值替换和。
回答by Sean Powell
You'll want to use what's called a Regular Expression.
您将需要使用所谓的正则表达式。
You should write a regular expression, and then use one of PHP's various regexp functionsto do what you want.
您应该编写一个正则表达式,然后使用 PHP 的各种regexp 函数之一来执行您想要的操作。
In this case, you should probably use the preg_replace() function, which finds a string that matches your regular expression and replaces it as you specify.
在这种情况下,您可能应该使用 preg_replace() 函数,它会查找与您的正则表达式匹配的字符串并按照您的指定替换它。
The regular expression you are looking for is particularly tricky to write, as URLs can come in many forms, but I found an expression which should do the trick:
您正在寻找的正则表达式编写起来特别棘手,因为 URL 可以有多种形式,但我找到了一个可以解决问题的表达式:
$text = "derp derp http://www.google.com derp";
$text = preg_replace(
'@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
'[yourtag][/yourtag]',
$text
);
echo $text;
This will output:
这将输出:
derp derp [yourtag]http://www.google.com[/yourtag] derp
You can see that the preg_replace()
function found the URL (and it will find multiple) in $text
, and put the tags I specified around it.
您可以看到该preg_replace()
函数在 中找到了 URL(并且会找到多个)$text
,并将我指定的标签放在它周围。
回答by NukeOoVeRQc
$text = " Helloooo try thiss http://www.google.com and www.youtube.com :D it works :)";
$text = preg_replace('#http://[a-z0-9._/-]+#i', '<a href="##代码##">##代码##</a>', $text);
$regex = "#[ ]+(www.([a-z0-9._-]+))#i";
$text = preg_replace($regex," <a href='http://'></a>",$text);
echo $text;