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
https://forums.phpfreaks.com/topic/254920-sorting-images-from-links/
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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.