Link to home
Start Free TrialLog in
Avatar of cimmer
cimmer

asked on

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;
}


Avatar of y2kwacko
y2kwacko

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

Avatar of cimmer

ASKER

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??
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
Avatar of cimmer

ASKER

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

the closing tags arent converting correctly. It still doesnt work.
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
Avatar of cimmer

ASKER

closing tags still arent closed...
ASKER CERTIFIED SOLUTION
Avatar of y2kwacko
y2kwacko

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
Avatar of cimmer

ASKER

awesome