m0tSiE
asked on
preg_replace problem
Hi,
Can someone please help me out with converting the following to html?
I have bbcode for a link that looks like this:
text
And I want to convert it to:
<a href="url">text</a>
I tried using this:
[Code Attached]
But the result I get is this:
<a href="%22url">text</a>
Due to the %22 it adds the current url to the beginning of the link and I cant seem to fix it.
Can anyone see where I'm going wrong or offer a better solution?
Thanks,
Paul
Can someone please help me out with converting the following to html?
I have bbcode for a link that looks like this:
text
And I want to convert it to:
<a href="url">text</a>
I tried using this:
[Code Attached]
But the result I get is this:
<a href="%22url">text</a>
Due to the %22 it adds the current url to the beginning of the link and I cant seem to fix it.
Can anyone see where I'm going wrong or offer a better solution?
Thanks,
Paul
$originalpost = array("[url=", "[/url]", "]");
$replacedpost = array("<a href=", "</a>", ">");
$newpost = str_replace($originalpost, $replacedpost, $post);
You can't use str_replace on a whole array but only to its elements.
The lionk you psted is
http://url/
but you're talking about a link such as
Please post the actual link you are trying to edit (assuming you want get
http://url/
but you're talking about a link such as
url=url[/url]
Please post the actual link you are trying to edit (assuming you want get
<a href="the_url">text</a>
Excuse me for my first comment: I was wrong :-(
But I would like to know what is $post: without knowing it I don't understand your code...
ASKER
url was just in place of any link such as http://google.com.
So the output currently shows as <a href="%22http://google.com">Google</a>
So the output currently shows as <a href="%22http://google.com">Google</a>
I meant
$post = "[url='http://myurl]text[/url]";
check this
$str="http://google.com";
echo substr($str,3 ,strlen($str));
or use
$str=trim("http://google.com");
echo $str
$str="http://google.com";
echo substr($str,3 ,strlen($str));
or use
$str=trim("http://google.com");
echo $str
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function url($url) {
$url = preg_replace('~[^\\pL0-9_] +~u%', '', $url);
$url = trim($url, "-");
$url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
$url = strtolower($url);
$url = preg_replace('~[^-a-z0-9_] +~', '', $url);
return $url;
}
$url = preg_replace('~[^\\pL0-9_]
$url = trim($url, "-");
$url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
$url = strtolower($url);
$url = preg_replace('~[^-a-z0-9_]
return $url;
}
or use
echo str_replace("", "%22", $url);
echo str_replace("", "%22", $url);
ASKER
Thanks, I the problem was the " was stored as ".
[url='http://google.com']text[/url]
a single quote is missing