Log in

View Full Version : PCRE, looking for a pattern to match and/or/both, but not neither



james438
10-24-2019, 06:21 AM
I'm not quite sure how to search for an answer to this, but I am trying to find a way to match one thing or another or both, but not none.

For example, I want to match a hyperlink if it begins with www or https:// or both, but ignore it if it is not preceded by either.


www.somesite.com //match
https://somesite.com //match
https://www.somesite.com //match
somesite.com //fail

any thoughts?

EDIT:

Here's the code that I use that works:


$summary[$i] = preg_replace('/(?<!http:\/\/|https:\/\/|\"|=|\'|\'>|\">)(www\..*?)(\s|\Z|\.\Z|\.\s|\<|\>|,)/i',"<a href=\"http://$1\">$1</a>$2",$summary[$i]);
$summary[$i] = preg_replace('/(?<!\"|=|\'|\'>|\">|site:)(https?:\/\/(www){0,1}.*?)(\s|\Z|\.\Z|\.\s|\<|\>|,)/i',"<a href=\"$1\">$1</a>$3",$summary[$i]);

The above code was divided into two because I could not apply the "and/or, but not both" rule. It is the code I wrote to hyperlink urls in the way that I want.