Jump to content

Ereg_replace


djp1988
 Share

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.

Edited by FirefoxRocks
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...