SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search and replace on a navigation string

    I need to do a search and replace (possibly using a regular expression search) to append ?pb=1 to every link found in a string. For example in the following html I want to make:-

    href="http://mynerja.com/about/balcon-de-europa-nerja/"
    into
    href="http://mynerja.com/about/balcon-de-europa-nerja/?pb=1".

    I need to do this dynamically in PHP for all the links in the string.

    Thanks for any suggestions and I hope the above makes sense.

    Code:
    <li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/"><span>Balcon de Europa, Nerja</span></a></li>
    	<li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
    	<li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,321
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi there,

    If we define a 'link' as what comes between href=" and the closing "
    then we can use the following regex:

    PHP Code:
    /href="(.*?)"
    This would give us:

    PHP Code:
    <?php 
    $String 
    ='<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/"><span>Balcon de Europa, Nerja</span></a></li>
              <li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
              <li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>'
    ;

    echo 
    preg_replace('/href="(.*?)"/s''href="$1?pb=1"'$String);
    which should hopefully do what you want.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Pullo - that looks like exactly what I need. I will give it a try and report back.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    which should hopefully do what you want.
    Finally had a chance to try this out this morning and it worked first time.

    Thanks again for your help.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,321
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    You're welcome.
    Thanks for taking the time to follow up

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Just keep in mind a caveat. This works fine as long as none of the links in your string have a querystring on them already.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,321
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Quote Originally Posted by StarLion View Post
    Just keep in mind a caveat. This works fine as long as none of the links in your string have a querystring on them already.
    Good point.
    How would one go about writing a reg ex that took that into account?

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    same regex, but feed the result into a callback function with preg_replace_callback
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,321
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi StarLion,

    What do you think about this solution?
    It works, but am I horribly overcomplicating things?

    PHP Code:
    <?php
    $String 
    ='<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/?param=1"><span>Balcon de Europa, Nerja</span></a></li>
              <li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
              <li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>'
    ;

    function 
    processURL($matches){
      
    $queryString = (array_key_exists('query'parse_url($matches[2])))? "&pb=1" "?pb=1";
      return 
    "$matches[1]$matches[2]$queryString$matches[3]";
    }

    echo 
    preg_replace_callback('/(<li.*?href=")(.*?)(".*?<\/li>)/''processURL'$String);
    ?>
    You will notice I've attached a querystring to the first link.

    Would be grateful for any feedback.

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    keep the same regex - you only care about the href attributes, so 'href="(.*?)"' will suffice for a pattern, and then the replace would just be
    PHP Code:
    'href="'.$matches[1].$querystring.'"' 
    (the href and quotes are static elements - so you dont need to pattern them)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,321
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Oh cool!
    That's much cleaner. Thanks!

    Here's the revised code in case the OP can make use of it:

    PHP Code:
    <?php
    $String 
    ='<li class="page_item page-item-1225"><a class="fadeThis" href="http://mynerja.com/about/balcon-de-europa-nerja/?param=1"><span>Balcon de Europa, Nerja</span></a></li>
              <li class="page_item page-item-1452"><a class="fadeThis" href="http://mynerja.com/about/beaches-in-nerja/"><span>Beaches in Nerja</span></a></li>
              <li class="page_item page-item-2958"><a class="fadeThis" href="http://mynerja.com/about/charities-in-nerja/"><span>Charities in Nerja CAS, Nerja Donkey Sanctuary, CUDECA</span></a></li>'
    ;

    function 
    processURL($matches){
      
    $queryString = (array_key_exists('query'parse_url($matches[1])))? "&pb=1" "?pb=1";
      return 
    'href="'.$matches[1].$queryString.'"';
    }

    echo 
    preg_replace_callback('/href="(.*?)"/s''processURL'$String);
    ?>


Bookmarks

Posting Permissions

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