Simple Regex help, need a minor modification to this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamwil
    New Member
    • May 2009
    • 13

    #1

    Simple Regex help, need a minor modification to this

    What up friends, I'm using this little function to parse hyperlinks in plain text and convert them into links. It goes like so
    Code:
    private function hyperlink($text) {
    		// match protocol://address/path/
    	    $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);
    	
    	    // match www.something
    	    $text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);
    	    
    	    // match @replys
    	    $text = preg_replace("/[@]+[A-Za-z0-9-_]+/", "<a href=\"http://twitter.com/\\0\">\\0</a>", $text);
    	    
    	    // match #hashtags
    	     $text = preg_replace("/[#]+[A-Za-z0-9-_]+/", "<a href=\"http://search.twitter.com/search?q=\\0\">\\0</a>", $text);
    	    
    	    // fix Twitter URLs
    	    $text= str_replace("<a href=\"http://twitter.com/@","<a target=\"_blank\" href=\"http://twitter.com/",$text);
    	    $text= str_replace("<a href=\"http://search.twitter.com/search?q=#","<a target=\"_blank\" class=\"hashtag\" href=\"http://search.twitter.com/search?q=%23",$text);
    	
    	    // return $text
    	    return $text;
    	}
    ...This works great however it doesn't parse URL's that have query strings at the end such as . If somebody could modify it to make it work with those I would be extremely grateful. Also, if it's not to difficult, it would be extremely beneficial to have it parse blip urls in the formThanks a bunch guys!
    Last edited by Markus; May 31 '09, 10:27 AM. Reason: We are not pornstars.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Check out example #3 in the manual entry for ereg_replace.
    Should work for both of your strings.

    Comment

    • jamwil
      New Member
      • May 2009
      • 13

      #3
      Cool I'll give that a shot.. I had originally gone there to find the solution, and ended up using the one that is posted 2 down from there as the guy claims the original code was wrong.

      I'll try out that code and see how she goes.

      Thanks a bunch!

      Comment

      • jamwil
        New Member
        • May 2009
        • 13

        #4
        Works like a charm... Thanks!

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          The PHP manual entries are pretty much always right.
          The comments, however, aren't always as dependable.

          Anyways, I'm glad you've found a solution :)

          Comment

          Working...