xyn Posted March 11, 2008 Share Posted March 11, 2008 Hey, Can someone please help me with an if statement. Basically In regular expressions i understan ?() is if then and ?()| is if then else. However when i use it in my bb code I dont understand how they work Just wonder if someone could shwo me cupple of examples? my code: "/\[(color|Color|COLOR)=?()[/b[\](.*)/" In my example, the bold text is my "variable" which i have got working. however my if statment needs to read as follows: If(word is between = and ]) set as var (.*) thanks. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 whats that surpose to do!!! also use the parameter /i to ignore case instead of trying to cover case versions (on color) heres a simple BB to html code snip your need to remove the extra - as if i didn't add them it will look messedup on this forum <?php // A simple FAST parser to convert BBCode to HTML // Trade-in more restrictive grammar for speed and simplicty // // Syntax Sample: // -------------- // [-img]http://phpfreaks.com/images/deadrats.gif[-/img] // [-url="http://phpfreaks.com"]phpfreaks[-/url] // [-mail="webmasterphpfreaks.com"]Webmaster[-/mail] // [-size="25"]HUGE[-/size] // [-color="red"]RED[-/color] // [-b]bold[-/b] // [-i]italic[-/i] // [-u]underline[-/u] // [-list][-*]item[-*]item[-*]item[-/list] // [-code]value="123";[-/code] // [-quote]John said yadda yadda yadda[-/quote] // // Usage: // ------ // <?php include 'bb2html.php'; ?> // <?php $htmltext = bb2html($bbtext); ?> function bb2html($text) { $bbcode = array("-<", "->", "[-list]", "[-*]", "[-/list]", "[-img]", "[-/img]", "[-b]", "[-/b]", "[-u]", "[-/u]", "[-i]", "[-/i]", '[-color="', "[-/color]", "[-size=\"", "[-/size]", '[-url="', "[-/url]", "[-mail=\"", "[-/mail]", "[-code]", "[-/code]", "[-quote]", "[-/quote]", '"]'); $htmlcode = array("<", ">", "<ul>", "<li>", "</ul>", "<img src=\"", "\">", "<b>", "</b>", "<u>", "</u>", "<i>", "</i>", "<span style=\"color:", "</span>", "<span style=\"font-size:", "</span>", '<a href="', "</a>", "<a href=\"mailto:", "</a>", "<code>", "</code>", "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>", '">'); $newtext = str_replace($bbcode, $htmlcode, $text); $newtext = nl2br($newtext);//second pass return $newtext; } ?> Quote Link to comment Share on other sites More sharing options...
xyn Posted March 11, 2008 Author Share Posted March 11, 2008 thanks, also I only want those cases doesnt get replaced. i onyl want either or as well as using preg_replace on windows with php 4.3. Still would like some pointers on using an if statement too. I like learning new things Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 to use the OR try this '/\[(size|Size|SIZE)=50\]/' Oh and FYI this post should of been posted here Quote Link to comment Share on other sites More sharing options...
xyn Posted March 11, 2008 Author Share Posted March 11, 2008 the 50 needs to be static. the thing is when i use these quotes get mixed up.. so i tried grouping them and nothing. I recon the statement will work. And I didnt actually know there was a specific forum for regular expressions. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 what are you trying to do? what you stuck on? why would you group them ? Quote Link to comment Share on other sites More sharing options...
xyn Posted March 11, 2008 Author Share Posted March 11, 2008 All i want to do is this... or and with regex i want to get the content between ="(.*)" as a variable. however if i put [-color=red][-size=20] (exclude -) i get this <span style='color:red"][size=16;'> so i need an if statement to STOP the variable before ] which will stop this mix up Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 do mean like this ? <?php $data = '[size="12"]this is my test[\size]'; $newdata = preg_replace('/\[size="(\d+)"\](.*?)\[\\\\size\]/', '<span style=\'size:"\1";\'>\2</span>', $data); echo $newdata; ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 or even <?php if (preg_match('/\[size="(\d+)"\](.*?)\[\\\\size\]/', $data, $regs)) { $vars = $regs[0]; } print_r($vars); ?> Quote Link to comment Share on other sites More sharing options...
xyn Posted March 11, 2008 Author Share Posted March 11, 2008 this will be easier... to show you what im trying to do <? require_once "class.bbcode.php"; $bb =new BBcode; $bb->bbSettings(array("bbenabled" =>0,"allowflash" =>true)); $s ="[color=\"red\"][size=\"16\"]Hey[/size] [strike]um[/strike][/color] [b]Name[/b],\n\n"; $s .="<br>How are [u][b][i]you[/i][/b][/u]; \n\n"; print $bb->bbDisplay($s); ?> this is the class i was producing: <?php class BBcode { function bbSettings($array =false){ $this->bbCodeEnabled =$array['bbenabled']; $this->bbFlashEnabled =$array['allowflash']; $this->bbHyperlinkEnabled =$array['allowlinks']; $this->bbImagesEnabled =$array['allowimages']; } function bbDisplay($str){ $bulktext =explode(" ",$str); foreach($bulktext as $word) $phrase[] =preg_replace($this->BBcode,$this->BBstyle,$word); return implode(" ",$phrase); } var $BBcode =array( "/\[[bb]\](.*)/","/\[\/[bb]\]/", "/\[[uu]\](.*)/","/\[\/[uu]\]/", "/\[[ii]\](.*)/","/\[\/[ii]\]/", "/\[(strike|Strike|STRIKE)\](.*)/","/\[\/(strike|Strike|STRIKE)\]/", "/\[(color|Color|COLOR)=\"(.*)\"\](.*)/","/\[\/(color|Color|COLOR)\]/", "/\[(size|Size|SIZE)=\"(\d+)\"\](.*?)/","/\[\/(size|Size|SIZE)\]/", ); var $BBstyle =array( "<span style='font-weight:bold;'>\\1","</span>", "<span style='text-decoration:underline;'>\\1","</span>", "<span style='font-style:italic;'>\\1","</span>", "<span style='text-decoration:line-through;'>\\2","</span>", "<span style='color:\\2;'>\\3","</span>", "<span style='font-size:\\2pt;'>\\3","</span>" ); var $BBReference =array("b","u","i","strike","color","colour"); } ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 you need lazyness on the color and strike "/\[(color|Color|COLOR)=\"(.*?)\"\](.*?)/","/\[\/(color|Color|COLOR)\]/", and "/\[(strike|Strike|STRIKE)\](.*?)/","/\[\/(strike|Strike|STRIKE)\]/", final code <?php //require_once "class.bbcode.php"; $bb =new BBcode; $bb->bbSettings(array("bbenabled" =>0,"allowflash" =>true)); $s ="[color=\"red\"][size=\"16\"]Hey[/size] [strike]um[/strike][/color] [b]Name[/b],\n\n"; $s .="<br>How are [u][b][i]you[/i][/b][/u]; \n\n"; print $bb->bbDisplay($s); class BBcode { function bbSettings($array =false){ $this->bbCodeEnabled =$array['bbenabled']; $this->bbFlashEnabled =$array['allowflash']; $this->bbHyperlinkEnabled =$array['allowlinks']; $this->bbImagesEnabled =$array['allowimages']; } function bbDisplay($str){ $bulktext =explode(" ",$str); foreach($bulktext as $word) $phrase[] =preg_replace($this->BBcode,$this->BBstyle,$word); return implode(" ",$phrase); } var $BBcode =array( "/\[[bb]\](.*)/","/\[\/[bb]\]/", "/\[[uu]\](.*)/","/\[\/[uu]\]/", "/\[[ii]\](.*)/","/\[\/[ii]\]/", "/\[(strike|Strike|STRIKE)\](.*?)/","/\[\/(strike|Strike|STRIKE)\]/", "/\[(color|Color|COLOR)=\"(.*?)\"\](.*?)/","/\[\/(color|Color|COLOR)\]/", "/\[(size|Size|SIZE)=\"(\d+)\"\](.*?)/","/\[\/(size|Size|SIZE)\]/", ); var $BBstyle =array( "<span style='font-weight:bold;'>\\1","</span>", "<span style='text-decoration:underline;'>\\1","</span>", "<span style='font-style:italic;'>\\1","</span>", "<span style='text-decoration:line-through;'>\\2","</span>", "<span style='color:\\2;'>\\3","</span>", "<span style='font-size:\\2pt;'>\\3","</span>" ); var $BBReference =array("b","u","i","strike","color","colour"); } ?> infact you should use it on BUI as well Quote Link to comment Share on other sites More sharing options...
xyn Posted March 11, 2008 Author Share Posted March 11, 2008 cheers. (y) top dog. no topic solved? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 welcome Regular expressions can be a pain but also fun and VERY useful.. but becareful dont forget about str_replace its faster for simple replacments Quote Link to comment Share on other sites More sharing options...
xyn Posted March 11, 2008 Author Share Posted March 11, 2008 i was going to use it for [bUI] and that, just regex for multiple criterias tbh Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.