Jump to content

eregi_replace


phorcon3

Recommended Posts

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

<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

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.