Jump to content

Sorting images from links


merylvingien

Recommended Posts

Hi Folks, ive run up against a bit of a problem that i cant fathom out

 

I have a function that sorts links from a string and turns them into proper links to be displayed

 

function txt2link($text){
        $text = str_replace( "www.", "http://www.", $text );
        $text = str_replace( "http://http://www.", "http://www.", $text );
        $reg_exUrl = "/(http)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
        if(preg_match($reg_exUrl, $text, $url)) {
                   $text = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
        }
                   return ($text);
}

 

However, when i post a image from say photobucket with the tags, i want to be able to sort those into <img> tags

 

But this function interferes with the url as it finds the http://www. or the www. within the string

 

Any ideas how to get round this?

Link to comment
Share on other sites

As a quick-and-dirty solution, you can replace http in img with some temporary string (hxxp), then replace it back:

 

<?php
function txt2link($text){
        $text = str_replace( 'www.', 'http://www.', $text );
        $text = str_replace( 'http://http://www.', 'http://www.', $text );
        $reg_exUrl = "http://(\S+)";
        
        $text = preg_replace('~\[img=' . $reg_exUrl . '\]~i', '<img src="hxxp://$1" alt="">', $text);
        
        $text = preg_replace('~' . $reg_exUrl . '~', '<a href="$0" rel="nofollow">$0</a>', $text);
        
        $text = str_replace( 'src="hxxp://', 'src="http://', $text );
        return ($text);
}

echo txt2link('[img=http://example.com/image.png] text www.example.com/ http://subdomain.example.com/file.php?p=x text');
?>

 

Note that you can use ~ as a delimiter to avoid the leaning toothpicks. Top-level domain names can be longer than 3 characters (.info is popular now; future gTLDs will be even longer).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.