Home > Backend Development > PHP Tutorial > 新手又有正则有关问题需要请问

新手又有正则有关问题需要请问

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:58:54
Original
880 people have browsed it

新手又有正则问题需要请教!
新手,在打书上的文章发布工具时,用正则匹配URL出了点问题。

<br />$pattern = '/(http\:\/\/|https\:\/\/)?([a-zA-Z0-9-]+\.)+(com|cn|org|net)(\/[\w\%\&\=\'\"\?\/\.]*)?/';<br />$string = 'http://demo.demo.baidu.com';<br />preg_match_all($pattern,$string,$result);<br />
Copy after login

遍历$result的结果是
[code=php]
Array
(
[0] => Array
(
[0] => http://demo.demo.baidu.com
)
[1] => Array
(
[0] => http://
)

[2] => Array
(
[0] => baidu.
)
[3] => Array
(
[0] => com
)
[4] => Array
(
[0] =>
)
)
[/php]
第二个子表达式其实是匹配了’www.‘以及’baidu.‘,有两次匹配,但是返回的结果里只有一处匹配T.T
整个表达式是能够匹配整个URL的,但是我要用到第二个子表达式的两次匹配结果,现在只有一个,望各位指教问题出现在哪儿!
感谢!
------解决方案--------------------
(http\:\/\/<br><font color='#FF8000'>------解决方案--------------------</font><br>https\:\/\/)?(([a-zA-Z0-9-]+\.)+)+(com<br><font color='#FF8000'>------解决方案--------------------</font><br>cn<br><font color='#FF8000'>------解决方案--------------------</font><br>org<br><font color='#FF8000'>------解决方案--------------------</font><br>net)(\/[\w\%\&\=\'\"\?\/\.]*)?
Copy after login

------解决方案--------------------
$pattern = '/(http\:\/\/<br><font color='#FF8000'>------解决方案--------------------</font><br>https\:\/\/)?((?:[a-zA-Z0-9-]+\.)+)(com<br><font color='#FF8000'>------解决方案--------------------</font><br>cn<br><font color='#FF8000'>------解决方案--------------------</font><br>org<br><font color='#FF8000'>------解决方案--------------------</font><br>net)(\/[\w%&=\'"?\/.]*)?/';<br />$string = 'http://demo.demo.baidu.com';<br />preg_match_all($pattern,$string,$result);<br />
Copy after login
Array<br />(<br />    [0] => Array<br />        (<br />            [0] => http://demo.demo.baidu.com<br />        )<br /><br />    [1] => Array<br />        (<br />            [0] => http://<br />        )<br /><br />    [2] => Array<br />        (<br />            [0] => demo.demo.baidu.<br />        )<br /><br />    [3] => Array<br />        (<br />            [0] => com<br />        )<br /><br />    [4] => Array<br />        (<br />            [0] => <br />        )<br /><br />)<br /><br />
Copy after login

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template