phorcon3 Posted April 17, 2008 Share Posted April 17, 2008 a) <?php $replace = 'Dude, you gotta check out this link: http://www.google.com!'; string = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $replace); return $string; ?> this should return: Dude, you gotta check out this link: <a href="http://www.google.com">http://www.google.com</a>! Note: www.google.com and http://www.google.com should both be recognized b) <?php $replace = 'Dude, you gotta check out this image: http://www.google.com/intl/en_ALL/images/logo.gif!'; string = eregi_replace('/^[a-zA-Z0-9_]+[.](jpg|gif|png)$', '<a href="\\0"><img src="\\0" border="0" /></a>', $replace); return $string; ?> this should return: Dude, you gotta check out this image: <a href="http://www.google.com/intl/en_ALL/images/logo.gif"><img src="http://www.google.com/intl/en_ALL/images/logo.gif" border="0" /></a>! Note: only allow .jpg, .gif and .png for images someone please help me with this:( Link to comment https://forums.phpfreaks.com/topic/101573-eregi_replace/ Share on other sites More sharing options...
ucffool Posted April 17, 2008 Share Posted April 17, 2008 For both of them, in line 4 you forgot a $, it should be: $string = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $replace); your second regex i need to work on more, I can't figure a good way yet. Link to comment https://forums.phpfreaks.com/topic/101573-eregi_replace/#findComment-519998 Share on other sites More sharing options...
effigy Posted April 18, 2008 Share Posted April 18, 2008 <pre> <?php $data = <<<DATA Dude, you gotta check out this link: http://www.google.com! Dude, you gotta check out this image: http://www.google.com/intl/en_ALL/images/logo.gif! DATA; function urlify($matches) { ### Images. if (preg_match('/\.(?:jpg|gif|png)\z/', $matches[0])) { return '<a href="' . urlencode($matches[0]) . '">' . '<img src="' . $matches[0] . '" border="0">' . '</a>'; } ### Non-images. else { return '<a href="' . urlencode($matches[0]) . '">' . $matches[0] . '</a>'; } } echo preg_replace_callback('% ### Protocol or start. (?: (??:https?|ftp)://) | www\. ) ### Body. (?: ### Gobble all non-whitespace. \S+ ### Avoid ending punctuation. (?<!\p{P}) ) %x', 'urlify', $data); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/101573-eregi_replace/#findComment-520390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.