2011-08-22 215 views
0

我正在寻找一个可以匹配url的模式。url匹配正则表达式为php

所有这些将包含“.no”,因为只有挪威域输入。

我觉得需要什么是这样的:

搜索之前和之后“没有”空格或断行,而这场比赛将是一个链接。

它应该匹配(与它周围的所有文本)什么的一些例子:

test.no 
test.no/blablabla/ 
test.no/blablabla/test.html 
test.no/blablabla/test.php 
test.no/blablabla/test.htm 

,这应该然后用

<a href="http://www.MATCH">MATCH</a> 

任何人都代替可以算出来?

+0

你试过[google搜索](http://www.google.ru/#q=URL+REGEX&oq=URL+REGEX)或[搜索](http://stackoverflow.com/search?q=URL+regex)? – J0HN

+0

是的,但我发现所有的例子都包含像“.com”这样的东西,并且对于我需要的东西来说似乎过于复杂。 –

+0

网址是一件非常复杂的事情。查看我的回答以及Gruber指定的(有效)[测试URL](http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text)。 – akirk

回答

1

这应该这样做:。

$html = preg_replace("#\w+\.no[\w/.-]*#", '<a href="http://www.$0">$0</a>', $html); 
+0

hm that does not match'http:// www.smykkmybitchup.no/halskjeder/sterling-silver/sterling-silver-halskjede-nr-23.html' http://ideone.com/oiB6h –

+0

fixed(by adding a - 到[]部分的末尾) – akirk

0

检查John Gruber's Regular expression for URLs并从那里继续。

+0

似乎对我所需要的东西来说太复杂了。我的意思是,我需要匹配的是一串包含'.no'的字符,然后在空格或换行符处停止匹配。链接中的模式似乎不符合我的'test.no'。 –

0

我希望这是不够严谨: ^ [\ W \ d - \ \\。] +无* $