Jump to content

Ereg_replace


djp1988

Recommended Posts

$str = "http://www.google.com youtube(http://www.youtube.com) (http://www.google.com)";$str = ereg_replace("[^(youtube\()]http://[^<>[:space:]]+[[:alnum:]/]",		  "<a href=\"\\0\" target=\"_blank\">\\0</a>", $str);echo $str;

Okay, I'm looking to make a ereg_replace that'll replace url's with the <a> element, but I don't want to pick up on url's inside parentheses with youtube on the from, for that I have other plans, but I want to allow a url just inside parentheses with no string to the leftSo the first link I want active and the last, but not the middle one, anyone?

Link to comment
Share on other sites

Guest FirefoxRocks
<?php(string)$str = "http://facebook.com/"; // replace this whatever URL you use(bool)$isUrl = false;if(substr($str, 0, 7) === "http://") {	$isUrl = true;}elseif(substr($str, 0, 8) === "(http://"){	$isUrl = true;	$str = substr($str, 1, -1); // assuming there's also a parenthesis at the end}if($isUrl && !stristr($str, "youtube.com/")){	echo "<a href='$str' target='_blank'>$str</a>";}else {	echo $str;}unset($isUrl);?>

I just made this up from the top of my head so I haven't tested it yet, but it should work.EDIT:Please note that this URL will not be printed in the <a> format: http://example.com/youtube.com/'>http://example.com/youtube.com/ because the string "youtube.com/" is found. But http://example.com/youtube.com will be printed in the <a> format.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...