开发者

url matching regex for php

开发者 https://www.devze.com 2023-03-29 16:12 出处:网络
I\'m looking for a pattern that can match urls. All of them will contain \".no\" as there is only Norwegian domains input.

I'm looking for a pattern that can match urls.

All of them will contain ".no" as there is only Norwegian domains input.

I think whats needed is this:

search for a space or linebreak before and after '.no', and the match will be a link.

Some examples of what it should match (all with text around it):

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

and this should then be replaced with

<a href="http://www.MATCH">MATCH</a>
开发者_开发知识库

anyone can figure this out?


This should do it:

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


Check John Gruber's Regular expression for URLs and go on from there.


I hope this is strict enough: ^[\w\d-\.\\]+.no.*$

0

精彩评论

暂无评论...
验证码 换一张
取 消