merylvingien Posted January 12, 2012 Share Posted January 12, 2012 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 More sharing options...
abareplace Posted January 13, 2012 Share Posted January 13, 2012 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 https://forums.phpfreaks.com/topic/254920-sorting-images-from-links/#findComment-1307120 Share on other sites More sharing options...
merylvingien Posted January 13, 2012 Author Share Posted January 13, 2012 Many thanks, that works a treat, i must admit that i need to really study up on regex stuff, its not my strong point. Link to comment https://forums.phpfreaks.com/topic/254920-sorting-images-from-links/#findComment-1307135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.