preg_match_all to find all URL but exclude email

梦想与她 提交于 2019-12-04 17:42:44

An example solution without using too advanced features like assertions:

<?php

$text = 'ftp://web.com, ';
$text .= "Website: www.example.com, ";
$text .= "Contact us: http://www.example.com/cu?t=contactus#anchor, ";
$text .= "Email: contact@example.com";

$base = "((http|https|ftp|ftps)://)?([a-zA-Z0-9\-]*\.)+[a-zA-Z0-9]{2,4}(/[a-zA-Z0-9=.?&-]*)?";

$matches = array(); preg_match_all("#$base#", $text, $matches); var_dump($matches[0]);
$matches = array(); preg_match_all("#\s($base)#", " $text", $matches); var_dump($matches[1]);

?>

Output:

array(4) {
  [0]=>
  string(13) "ftp://web.com"
  [1]=>
  string(15) "www.example.com"
  [2]=>
  string(37) "http://www.example.com/cu?t=contactus"
  [3]=>
  string(11) "example.com"
}
array(3) {
  [0]=>
  string(13) "ftp://web.com"
  [1]=>
  string(15) "www.example.com"
  [2]=>
  string(37) "http://www.example.com/cu?t=contactus"
}

Simply check for whitespace before URL but not include it in subpattern. Using [^@] won't work because regex would simply match e as [^@] and xample.com as the rest of the match - they would be later merged into a single match.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!