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
Share on other sites

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