Just a quick regular expression question!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gene.ellis@gmail.com

    Just a quick regular expression question!

    Hello everyone,

    This should be extremely simple for youregular expression gurus out
    there. I am looking for a regular expression that will look for the
    hyperlinks within a string, and then add some simple code before the
    hyperlinks, but only if they do not match a certain URL. For example.

    $string ="This is a <a href='http://www.ur1.com'>Hy perlink</a> and this
    is a <a href='http://www.ur2.com'> Hyperlink</a>";

    I would like to a regular expression to look in the string for
    hyperlinks that start with "http://www.ur1" and change add some code to
    it like so that the string is modified like:

    $string ="This is a <a
    href=javascript :'http://www.ur1.com'>Hy perlink</a> and this is a <a
    href='http://www.ur2.com'> Hyperlink</a>";

    That's it. Thank you very much for your help!

  • Sjoerd

    #2
    Re: Just a quick regular expression question!

    Couldn't you find anything on Google, for example by searching on
    "regex url"? If you want to append .com to links, try using
    preg_replace.

    Comment

    • bala

      #3
      Re: Just a quick regular expression question!

      Hi Gene...

      Try this....if u have found any pbm ,mail me

      $string ="This is a <a href='http://www.ur1.com'>Hy perlink</a> and this

      is a <a href='http://www.ur2.com'> Hyperlink</a>";

      echo 'Before => '.$string;

      $string = ereg_replace('( .*)(http:\/\/www.ur1)(.*)',
      '\\1javascript: \\2\\3', $string);

      echo '<br>After => '.$string;

      Comment

      • gene.ellis@gmail.com

        #4
        Re: Just a quick regular expression question!

        Thanks! Just one more querstion. What if I wanted to match all
        hyperlinks that DO NOT match a certain domain. Such as, what if I
        wanted to find all URLS that do not match http://www.url1

        Comment

        • gene.ellis@gmail.com

          #5
          Re: Just a quick regular expression question!

          so this is my regular expression:


          $string_sep=pre g_match_all('/(http:\/\/.+\.com)/',$page_content ,$match,PREG_SE T_ORDER);
          for ($i = 0; $i<count($match ); $i++) {
          $url = $match[$i][0];
          ereg('http:\/\/www\.(.*)\.com' , $url, $out);
          if ($out[1] != 'uctltc') {
          // echo $url.'<br>';
          $page_content =
          ereg_replace("$ url","javascrip t:launch('http://www.uctltc.org/common/opener.php?url= $url','window1' )","$page_conte nt");
          }
          }

          It works! However, I need it to not only look through .com URLS, but be
          open to the other extensions as well (.org, .net, .etc). And I want it
          to only exampline hyperlink URLS, not image URLS, or any other sort of
          URL. Thanks so much for your help! It is almost done!

          Comment

          Working...