If you have any questions about regular expressions, please consult! Newbie, when posting an article publishing tool on the book, there is something wrong with the regular expression matching URL. $ Pattern & nbsp; (| )? [A-zA-Z0-9-] +.) + (com | cn | org | net) ([w % & amp;?.] *)?; $ String & nbsp; for beginners, you need to consult if you have any regular expressions!
Newbie, when posting an article publishing tool on the book, there is something wrong with the regular expression matching URL.
$pattern = '/(http\:\/\/|https\:\/\/)?([a-zA-Z0-9-]+\.)+(com|cn|org|net)(\/[\w\%\&\=\'\"\?\/\.]*)?/';
$string = 'http://demo.demo.baidu.com';
preg_match_all($pattern,$string,$result);
The result of traversing $ result is
[Code = php]
Array
(
[0] => Array
(
Http://demo.demo.baidu.com
)
[1] => Array
(
[0] => http ://
)
[2] => Array
(
[0] => baidu.
)
[3] => Array
(
[0] => com
)
[4] => Array
(
[0] =>
)
)
[/Php]
The second subexpression actually matches 'www. 'and 'Baidu.', but only one matching T.T is returned.
The whole expression can match the whole URL, but I want to use the second subexpression to match the result twice. now there is only one. please advise where the problem is!
Thank you!
------ Solution --------------------
(Http \:\/\/
------ Solution --------------------
Https \:\/\/)? ([A-zA-Z0-9-] + \.) + (com
------ Solution --------------------
Cn
------ Solution --------------------
Org
------ Solution --------------------
Net) (\/[\ w \ % \ & \ = \'\"\? \/\.] *)?
------ Solution --------------------
$ Pattern = '/(http \:\/\/
------ Solution --------------------
Https \:\/\/)? ((? : [A-zA-Z0-9-] + \.) +) (com
------ Solution --------------------
Cn
------ Solution --------------------
Org
------ Solution --------------------
Net) (\/[\ w % & = \'"? \/.] *)? /';
$ String = 'http: // demo.demo.baidu.com ';
Preg_match_all ($ pattern, $ string, $ result );
Array
(
[0] => Array
(
[0] => http://demo.demo.baidu.com
)
[1] => Array
(
[0] => http://
)
[2] => Array
(
[0] => demo.demo.baidu.
)
[3] => Array
(
[0] => com
)
[4] => Array
(
[0] =>
)
)