SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member wfareed's Avatar
    Join Date
    Dec 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting text URLS into real clickable links

    I am trying to create a script that will help me convert urls provided by users in their blog posts of PM to other users into a real clickable links , The script i created is working fine except that it is not converting after the 2nd link occurred in the post , I hope someone can help me with that.
    Here is the code :

    PHP Code:

    $text
    ="Click [link]http://www.google.com.eg[/link] or click [link]http://www.yahoo.com[/link] or click [link]http://www.hotmail.com[/link]";

    preg_match_all('/\\[link](.*?)\\[\/link]/s'$text$links);

    $link_count=count($links);
    for(
    $i=0;$i<$link_count;$i++){
        
    $link_url=preg_replace("/\[link]/"""$links[0][$i]);
        
    $link_url=preg_replace("/\[\/link]/","",$link_url);
        
    $text=str_replace($links[0][$i],"<a href=\"" $link_url "\">" $link_url "</a>",$text);
    }

    echo 
    $text

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think it is a clash of double/single quotes ...
    SB
    PHP Code:
    $text=str_replace($links[0][$i],'<a href=\"' $link_url '\">' $link_url '</a>'$text); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2012
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're accessing the wrong portion of the array returned by preg_match_all(). $link[0] contains the entire match, not the just the link you're targeting with the regex subpattern. Loop through $link[1]. That has all the URL's you want, already stripped of formatting. There's no need to run two more regular expressions as well as the str_replace().

    Also, your regex expression could be tidied up a bit.


    Here you go:
    Code:
    preg_match_all('~\[link](.*?)\[/link]~s', $text, $links); 
    $text = '';
    foreach($links[1] as $key => $link)
    {
    	$text .= "<a href=\"" . $link . "\">" . $link . "</a> ";
    }

  4. #4
    SitePoint Member wfareed's Avatar
    Join Date
    Dec 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot , That was pretty helpful

  5. #5
    SitePoint Member wfareed's Avatar
    Join Date
    Dec 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your help , appreciate it

  6. #6
    SitePoint Member
    Join Date
    Oct 2012
    Location
    San Diego, Ca
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I'm new to the forum and posted the same question.
    I'm also new to PHP and would like to use your script but not sure how to add it to my stie.

    Thanks


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
  •