Preg Replace acting very strange ???

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Preg Replace acting very strange ???

    I Have A string that has got 2 links in it that are identical:

    $str = '<a href="-randomtext-static-words-948191.html"> somewhere </a>
    more text here ....
    <a href="-randomtext-static-words-948191.html"> somewhere </a>';

    im using Preg replace like this:

    $str = preg_replace('/<a href="-(.*?)-static-words-(.*?).html">/is','<a href="?page=-$1-static-words-$2.html">',$str);

    this should echo the same on both links but it only changes the second link:
    output:

    <a href="-randomtext-static-words-948191.html"> somewhere </a>
    more text here ....
    <a href="?page=-randomtext-static-words-948191.html"> somewhere </a>


    However if i change the preg replace to this:
    $str = preg_replace('/<a href="-(.*?)-static-words-(.*?).html">/is','<a href="?page=-$1-testing-123-$2.html">',$str);

    the output changes to:
    <a href="-randomtext-testing-123-948191.html"> somewhere </a>
    more text here ....
    <a href="?page=-randomtext-testing-123-948191.html"> somewhere </a>


    what the hell is going on??? .... how can preg replace match the expression and yet only replace half of the string??

    #2
    Originally posted by something else View Post
    I Have A string that has got 2 links in it that are identical:

    $str = '<a href="-randomtext-static-words-948191.html"> somewhere </a>
    more text here ....
    <a href="-randomtext-static-words-948191.html"> somewhere </a>';

    im using Preg replace like this:

    $str = preg_replace('/<a rel="nofollow" href="-(.*?)-static-words-(.*?).html">/is','<a rel="nofollow" href="?page=-$1-static-words-$2.html">',$str);

    this should echo the same on both links but it only changes the second link:
    output:

    <a href="-randomtext-static-words-948191.html"> somewhere </a>
    more text here ....
    <a rel="nofollow" href="?page=-randomtext-static-words-948191.html"> somewhere </a>


    However if i change the preg replace to this:
    $str = preg_replace('/<a rel="nofollow" href="-(.*?)-static-words-(.*?).html">/is','<a rel="nofollow" href="?page=-$1-testing-123-$2.html">',$str);

    the output changes to:
    <a href="-randomtext-testing-123-948191.html"> somewhere </a>
    more text here ....
    <a rel="nofollow" href="?page=-randomtext-testing-123-948191.html"> somewhere </a>


    what the hell is going on??? .... how can preg replace match the expression and yet only replace half of the string??
    a php bug?

    with
    PHP Code:
    $str '<a href="-randomtext-static-words-948191.html"> somewhere </a>

     more text here ....
     
    <a href="-randomtext-static-words-948191.html"> somewhere </a>'
    ;

    $str preg_replace('/<a href="-(.*?)-static-words-(.*?).html">/is','<a href="?page=-$1-static-words-$2.html">',$str); 
    i get this as output:
    PHP Code:
    <a href="?page=-randomtext-static-words-948191.html"somewhere </amore text here .... <a href="?page=-randomtext-static-words-948191.html"somewhere </a
    Added after 3 minutes:

    Im not sure but try to add limit -1 to your preg replace
    like
    PHP Code:
    $str preg_replace('/<a href="-(.*?)-static-words-(.*?).html">/is','<a href="?page=-$1-static-words-$2.html">',$str,-1); 
    Last edited by GumSlone; 13.12.11, 22:10.
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      i tried limiting it to 1 and it still didnt work....

      Here`s the script ....

      it does look like a bug in PHP to me unless im missing something..
      Attached Files

      Comment


        #4
        Try this bro with suppressed dot.
        PHP Code:
        $str preg_replace('/<a href="-(.*?)-static-words-(.*?)\.html">/is','<a href="?page=-$1-static-words-$2.html">',$str); 
        <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

        Comment


          #5
          it matched it again but still only replaces half the url ..... it really doesnt make sence

          Comment


            #6
            yeah strange **** i also tried to fix it, it din't replaced all manny ways none worked ..
            This is ten percent luck, twenty percent skill
            Fifteen percent concentrated power of will
            Five percent pleasure, fifty percent pain

            And a hundred percent reason to remember the name!

            Comment


              #7
              Originally posted by arnage View Post
              Try this bro with suppressed dot.
              PHP Code:
              $str preg_replace('/<a href="-(.*?)-static-words-(.*?)\.html">/is','<a href="?page=-$1-static-words-$2.html">',$str); 
              I think the answer is suppressed minus :D but havent tried it

              Comment


                #8
                Quantifiers are:
                \ | ( ) [ { ^ $ * + ? . < >

                Try this bro and if not there can only be problem in quotes with $1 or $2.

                PHP Code:
                $str preg_replace('/\<a href="-(.*?)-static-words-(.*?)\.html"\>/is','\<a href="\?page=-$1-static-words-$2\.html"\>',$str); 
                <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                Comment

                Working...
                X