doing a preg_replace in PHP - help needed!

Discussion in 'PHP' started by pylon, Dec 5, 2008.

  1. #1
    I have some chat software that uses this line of code to turn "http://www.someurl.com" into a hyperlink

    Here's the current code:
    $ret = preg_replace("#(^|[\n \]])([\w]+?://[\w\#$%&~/.\-;:=,?@+]*)#ise", "'\\1<a href=\"\\2\"" . $target . ">' . pfc_shorten_url('\\2') . '</a>'", $ret);

    I can't figure this out :confused: I was wondering if a pre_replace wiz could do something that matches a youtube link to a certain URL:
    like from
    http://www.youtube.com?v=DFSLKJ

    to

    <a href="#" onclick="opentube('http://www.youtube.com?v=DFSLKJ')"><img src="arbritrary_image"></a>
     
    pylon, Dec 5, 2008 IP
  2. CommuniTriper

    CommuniTriper Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    function riptube($link) {
    		$tubefunction = "<a href=\"#\" onclick=\"opentube('\\1')\"><img src=\"arbritrary_image\"></a>";
    		if (preg_match("/^http:\/\/www.youtube.com\?v=([A-Z]+)$/", $link)) {
    			return preg_replace("/(.+)/",$tubefunction, $link);
    			break;
    		} else {
    			return FALSE;
    			break;
    		}
    		return FALSE;
    	}
    
    Code (markup):
    If I understood you correctly, you mean something like this?
     
    CommuniTriper, Dec 6, 2008 IP
  3. pylon

    pylon Peon

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hmm not exactly, how about this

    right now this code:
    $ret = preg_replace("#(^|[\n \]])([\w]+?://[\w\#$%&~/.\-;:=,?@+]*)#ise", "'\\1<a href=\"\\2\"" . $target . ">' . pfc_shorten_url('\\2') . '</a>'", $ret);

    matches an "xxxx://yyyy" URL at the start of a line, or after a space.

    how would I modify it to match "xxxx://yyyyyoutube" instead? (the four x's and four y's stand for letters/symbols)
     
    pylon, Dec 6, 2008 IP
  4. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #4
    can you give me example...
    input string and your output string...
     
    misbah, Dec 6, 2008 IP
  5. pylon

    pylon Peon

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    with this code:

    $ret = preg_replace("#(^|[\n \]])([\w]+?://[\w\#$%&~/.\-;:=,?@+]*)#ise", "'\\1<a href=\"\\2\"" . $target . ">' . pfc_shorten_url('\\2') . '</a>'", $ret);

    input string:

    "blaa blaa arbitrary text http://www.website.com blaa blaa"

    output string:

    "blaa blaa arbitrary text <a href="http://www.website.com">http://www.website.com</a> blaa blaa

    i want to modify that pre_replace so that it only does it for youtube links
     
    pylon, Dec 6, 2008 IP
  6. pylon

    pylon Peon

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ah nevermind, I got it to work by doing this

    $ret = preg_replace("#(^|[\n \]])([\w]+?://www\.youtube[\w\#$%&~/.\-;:=,?@+]*)#ise", "'\\1<a href=\"\\2\"" . $target . ">' . pfc_shorten_url('\\2') . '</a>'", $ret);
     
    pylon, Dec 6, 2008 IP