FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Replacing a string of text with str_replace





jmlworld
I want to replace a text like this:

Code:
[url link="http://google.com" target="_blank"]Google[/url]


With the standard HTML one:

Code:
<a href="http://google.com" target="_blank">Google</a>


How can I do that with str_replace and regex?

Please help...
Aredon
Well you'd need to write a function to change each portion of the line, but surely since it is BB code and html there is a plugin somewhere that you could use instead?
Hogwarts
You want to use preg_replace, not str_replace.
blueray
Please try this ( not tested )

Code:

$pattern = "/([a-z\s\[]*)=([a-z:/"_\.\s=]*)(\]).([a-zA-Z]*)([a-z/\[\]]*)/i"
rickylau
blueray wrote:
Please try this ( not tested )

Code:

$pattern = "/([a-z\s\[]*)=([a-z:/"_\.\s=]*)(\]).([a-zA-Z]*)([a-z/\[\]]*)/i"

I'm trying to grasp this piece of code, I found that at least there will be syntax error around
Code:
a-z:/"_
rickylau
I have another idea, it matches the result of the sample provided.

Code:
<?php

$text = '[url link="http://google.com" target="_blank"]Google[/url]';

$replaces ['url'] [''] = 'a';
$replaces ['url'] ['link'] = 'href';

foreach ($replaces as $tag => $attributes) {
   // Replace attributes in known tags
   foreach ($attributes as $attribute => $htmlAttribute) {
      if ($attribute == '') { continue; }
      $text = preg_replace ('/(\['.$tag.'[^\]]+)'.$attribute.'=([^\]]+\])/i', '\1'.$htmlAttribute.'=\2', $text);
   }
   // Replace known tags
   $text = preg_replace ('/\[([\/]?)'.$tag.'([^\]]*)\]/i', '<\1'.$attributes [''].'\2>', $text);
}

echo $text;

?>


For convenience of modifying the code to process more replacements, I declare a variable replacements first to tell what tag and attributes have to be replaced and converted to HTML tags.

Code:
$replaces [$tag] [$attribute] = $htmlEquivalent;

That means attribute name $attribte of tag name $tag in bbCode should be replaced with $htmlEquivalent.
Empty $attribte means tag name $tag in bbCode should be replaced with $htmlEquivalent.

However, the performance of this way should be quite bad, but I have no other idea.
jmlworld
Thank you guys for the helpful assists.

@rickylau, I liked the way you constructed your code. It works well for me. Thanks for the help, mate.

I am currently using this code:

Code:
$text = '[url link="http://google.com" target="_blank"]Google[/url]';
echo preg_replace( '/\[url link=(.*)\](.*)\[\/url\]/Usi', '<a href=\\1
>\\2</a>', $text);


I wonder why guys at PHP official website didn't give much care on explaining regex well. This link on Perl website gave me an incredible explanation.

http://www.perl.com/doc/manual/html/pod/perlre.html
rickylau
jmlworld wrote:
I wonder why guys at PHP official website didn't give much care on explaining regex well.
Actually there is a section on perl's regular expressions syntax in PHP manual

http://hk.php.net/manual/en/pcre.pattern.php
jmlworld
rickylau wrote:
jmlworld wrote:
I wonder why guys at PHP official website didn't give much care on explaining regex well.
Actually there is a section on perl's regular expressions syntax in PHP manual

http://hk.php.net/manual/en/pcre.pattern.php


Thanks Rickylau for the link. The pattern syntax section (http://hk.php.net/manual/en/reference.pcre.pattern.syntax.php) explains all I wanted. I will play with it when I go home...
Related topics
Site Nav Suggestions...
PHP Tutorials
PHP - Find and Delete
Slapping a WaterMark Automatically.
cout alignment
[site]função para criar variavel
PHP Image functions
Store data format as user entry
Help SQL Sort
Extract all img tag from a template file
Templating
PHP inside HTML tags?
Need help with watermarks
problem with AJAX display
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.