Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

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


0
cimmer
Asked:
cimmer
  • 4
  • 4
1 Solution
 
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
Independent Software Vendors: 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!

 
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
 
cimmerAuthor Commented:
awesome
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now