-2

Possible Duplicate:
php regex <b> to <b>

Inside my admin panel I have a textarea where staff can insert messages to customers.

I want to convert and detect [[p=304_red]] to two matches within a return. I was thinking str_replace but I cannot do it twice. Also, str_replace would be limited to one, a staff member might add it twice. Anyone recommend a better solution (preg_replace seem tricky and messy)

[[p=304_red]] 

Should return

<a href="http://site.com/jump?go=304_red">
<img src="http://cdn.com/304_red/large/1.jpg" />
</a>

My code only returns the URL.

$question = $_GET['replyBoxField'];
$question = str_replace("[[p=", "<a href='http://site.com/jump?go='>", $question);
$question = str_replace("]]", "</a>", $question);
Community
  • 1
  • 1
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

3 Answers3

3

Simple regex should work for you here.

$question = preg_replace('@\[\[p=([^\]]+)\]\]@i', '<a href="http://site.com/jump?go=$1"><img src= "http://cdn.com/$1/large/1.jpg" ></a>', $question);

=)

cstrat
  • 1,007
  • 10
  • 17
0

Isn't regex a better approach?

$filter = "/\[\[p=([^\]\s]+)\]\]/";
$transform = "<a href='http://site.com/jump?go=$1'><img src='http://cdn.com/$1/large/1.jpg' /></a>";

$string = preg_replace($filter, $transform, $string);
nyson
  • 1,055
  • 6
  • 20
-1

All other answers wrong, see this:

Change your BBCode to this [[p=304_sapphire]#[304_sapphire]]

$question = str_replace("[[p=", "<a href='http://domain.com/jump?go=", $question);
$question = str_replace("]#[", "'><img src='http://cdn.com/files/", $question);
$question = str_replace("]]", "/1/small.jpg'/></a>", $question);
AlphaApp
  • 615
  • 1
  • 7
  • 15