Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    86
    Thanks
    0
    Thanked 2 Times in 2 Posts

    preg_replace and regex help - Please

    I wonder if someone would do me a small favor and help me with a regex problem? This code was written by somebody else but it works great for extracting link urls on a page. But I don't want it to look for ALL links, just the links that have this this beginning"

    /search.php?

    So actually the links would look like this

    <a href="/search.php?????">


    PHP Code:
    $source preg_replace("/a[\s]+[^>]*?href[\s]?=[\s\"\']+"."(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/"urlencode($source), $source); 
    Thanks!
    Last edited by Jinxy; 01-09-2010 at 01:20 AM.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,795
    Thanks
    19
    Thanked 156 Times in 147 Posts
    Code:
    /(<a href=")(\/search\.php\?.+?)(">)(.+?)(<\/a>)/
    Untested. Maybe something like that...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    '%<a [^>]+href="(?P<url>/search\.php\?[^"]+)"[^>*]*>(?P<text>[^< ]+)</a>%si'
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    86
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Thanks guys!

  • #5
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    238
    Thanks
    18
    Thanked 34 Times in 33 Posts
    All examples above are correct but I will suggest to make it more flexible, sine you could have three possible URLs on the page:
    Code:
    <a something href='/search.php?variables' something>
    <a something href="/search.php?variables" something>
    <a something href=/search.php?variables something>
    PHP Code:
    LINK => preg_match('#<a[^>]+href=(?:"|\'|)(/search\.php\?[^"\'\s]+)#'$data$values);
    TEXT => preg_match('#<a[^>]+href=(?:"|\'|)/search\.php\?[^>]+>([^<]+)#'$data$values); 

  • #6
    New Coder
    Join Date
    Feb 2007
    Posts
    86
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Thanks PHP6! That works great!


  •  

    Posting Permissions

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