javascript to php question

below is javascript.... What would be the PHP equivelent??  
it converts custom forum tags for display like...  
[br] to <br />  
[b:]abc[:b] to <b>abc</b>  
[a: href="anything"]abc[:a]  to  <a href="anything">abc</a>
[linebreak] to <br />
 
function replacetags(str){                                                str=str.replace(/\[(\w*)\]/gi,'<$1 />');                                    str=str.replace(/\[\:(\w*)\]/gi,'</$1>');
      str=str.replace(/\[(\w*)\:([^\]]*)\]/gi,'<$1$2>');
      str=str.replace(/\[linebreak\]/gi,'<br />');
      return str;
}


cimmerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

y2kwackoCommented:
Keep in mind that this code will for for ANY html tag so you may want to code in some safeguards to prevent it. It uses the perl regular expression replace function built into php. More information on this function can be found at http://us2.php.net/manual/en/function.preg-replace.php
----------------------------------------------------------------------------------------------------
$str = replacetags("[b]this is some bold text[/b]
[linebreak]
[br]
[linebreak]
[i]This is some italic text[/i]");

echo($str);

function replacetags($str){
      $str=preg_replace('/\[linebreak\]/i','<br />',$str);
      $str=preg_replace('/\[(\w*)\]/i','<$1>',$str);
      $str=preg_replace('/\[\/(\w*)\]/i','</$1>',$str);
      return $str;
}

----------------------------------------------------------------------------------------------------

Regards,
Kevin

0
cimmerAuthor Commented:
my example has colons...

[br] to <br />  
[b:]abc[:b] to <b>abc</b>  
[a: href="anything"]abc[:a]  to  <a href="anything">abc</a>

how do you add the colon into that??
0
y2kwackoCommented:
I don't quite see the point in the colons but here is your text/example...


----------------------------------------------------------------------------------------------------
$str = replacetags('[br] to <br />  
[b:]abc[:b] to <b>abc</b>  
[a: href="anything"]abc[:a]  to  <a href="anything">abc</a>');

echo($str);

function replacetags($str){
      $str=preg_replace('/\[linebreak\]/i','<br />',$str);
      $str=preg_replace('/\[(\w*)\]/i','<$1>',$str);
      $str=preg_replace('/\[(\w*)\:([^\]]*)\]/i','<$1$2>',$str);
      $str=preg_replace('/\[\/(\w*)\]/i','</$1>',$str);
      return $str;
}
----------------------------------------------------------------------------------------------------

The only difference is [br] is getting switched to <br> and not <br /> which shouldn't really matter.

Regards,
Kevin
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

cimmerAuthor Commented:
your example returns <br> to <b>abc<b> t<a href="anything">abc<a>...

the closing tags arent converting correctly. It still doesnt work.
0
y2kwackoCommented:
my apologies I thought you were trying to convert bbcode which uses [/b] as a closing.

----------------------------------------------------------------------------------------------------
$str = replacetags('[br] to <br />  
[b:]abc[:b] to <b>abc</b>  
[a: href="anything"]abc[:a]  to  <a href="anything">abc</a>');

echo($str);

function replacetags($str){
     $str=preg_replace('/\[linebreak\]/i','<br />',$str);
     $str=preg_replace('/\[(\w*)\]/i','<$1>',$str);
     $str=preg_replace('/\[(\w*)\:([^\]]*)\]/i','<$1$2>',$str);
     $str=preg_replace('/\[:(\w*)\]/i','</$1>',$str);
     return $str;
}
----------------------------------------------------------------------------------------------------

basically we just changed the last line before return $str to check for :tag instead of /tag
0
cimmerAuthor Commented:
closing tags still arent closed...
0
y2kwackoCommented:
$str = replacetags('[linebreak][br] to <br />  
[b:]abc[:b] to <b>abc</b>  
[a: href="anything"]abc[:a]  to  <a href="anything">abc</a>');

echo($str);

function replacetags($str){
      $str=preg_replace('/\[linebreak\]/','<br />',$str);
      $str=preg_replace('/\[\:(\w*)\]/','</$1>',$str);
      $str=preg_replace('/\[(\w*)\:([^\]]*)\]/','<$1$2>',$str);
      $str=preg_replace('/\[(\w*)\]/','<$1>',$str);
      return $str;
}


this time I tested it numerous times with numerous starting points
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cimmerAuthor Commented:
awesome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.