Results 1 to 1 of 1

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

  1. #1
    Join Date
    Jan 2007
    Davenport, Iowa
    Thanked 113 Times in 111 Posts

    Default PCRE, looking for a pattern to match and/or/both, but not neither

    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.

    Code:          //match      //match  //match              //fail
    any thoughts?


    Here's the code that I use that works:

    PHP Code:
    $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.
    Last edited by james438; 10-25-2019 at 05:24 AM. Reason: added more info.
    To choose the lesser of two evils is still to choose evil. My personal site

Similar Threads

  1. I need a preg_match pattern
    By mda_omega in forum PHP
    Replies: 9
    Last Post: 11-01-2010, 01:23 PM
  2. Recursive regex pattern replace
    By djr33 in forum PHP
    Replies: 12
    Last Post: 07-11-2010, 11:09 PM
  3. Replies: 3
    Last Post: 02-15-2010, 12:50 PM
  4. PHP with Mvc Design pattern using OOP
    By balaj in forum MySQL and other databases
    Replies: 0
    Last Post: 12-11-2008, 06:41 AM
  5. PHP perl and pattern matching
    By james438 in forum PHP
    Replies: 7
    Last Post: 06-11-2007, 08:34 PM


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts