Link to home
Start Free TrialLog in
Avatar of m0tSiE
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
$originalpost = array("[url=", "[/url]", "]");
$replacedpost = array("<a href=", "</a>", ">");

$newpost = str_replace($originalpost, $replacedpost, $post);

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

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
url=url[/url]

Open in new window


Please post the actual link you are trying to edit (assuming you want get  
<a href="the_url">text</a>

Open in new window

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...
Avatar of m0tSiE
m0tSiE

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>
Well, but your variable $post is something like

$post = "text";

?
I meant
$post = "[url='http://myurl]text[/url]";

Open in new window

check this
$str="http://google.com";
echo substr($str,3 ,strlen($str));
or use
$str=trim("http://google.com");
echo $str

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;  
}
or use
echo str_replace("", "%22", $url);
$post = "text";

you missed out a single quote
Avatar of m0tSiE

ASKER

Thanks, I the problem was the " was stored as &quot;.

[url='http://google.com']text[/url]

a single quote is missing

Open in new window