[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

preg_replace problem

Posted on 2011-10-25
15
Medium Priority
?
240 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:m0tSiE
  • 7
  • 3
  • 3
  • +1
15 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37024956
You can't use str_replace on a whole array but only to its elements.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37025009
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

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37025038
Excuse me for my first comment: I was wrong :-(
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37025060
But I would like to know what is $post: without knowing it I don't understand your code...
0
 

Author Comment

by:m0tSiE
ID: 37025067
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>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37025098
Well, but your variable $post is something like

$post = "text";

?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37025120
I meant
$post = "[url='http://myurl]text[/url]";

Open in new window

0
 
LVL 10

Expert Comment

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

0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 37025139
Using this code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');

$post = "[url='http://google.com]text[/url]";
$originalpost = array("[url=", "[/url]", "]");
$replacedpost = array("<a href=", "</a>", ">");

$newpost = str_replace($originalpost, $replacedpost, $post);
echo "newpost is" . htmlentities($newpost);
?>

Open in new window

ina blank page I get this:
newpost is<a href='http://google.com>text</a>

Open in new window

0
 
LVL 10

Expert Comment

by:ukerandi
ID: 37025166
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;  
}
0
 
LVL 10

Expert Comment

by:ukerandi
ID: 37025178
or use
echo str_replace("", "%22", $url);
0
 
LVL 11

Expert Comment

by:kelvinwkw
ID: 37025190
$post = "text";

you missed out a single quote
0
 
LVL 11

Expert Comment

by:kelvinwkw
ID: 37025210
0
 

Author Closing Comment

by:m0tSiE
ID: 37025216
Thanks, I the problem was the " was stored as &quot;.
0
 
LVL 11

Expert Comment

by:kelvinwkw
ID: 37025219

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

a single quote is missing

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question