RobertP Posted February 1, 2012 Share Posted February 1, 2012 i have 2 very simple concepts, and 1 very annoying problem that i think i have solved. I am posting here to be safe. My Original Concept $string = 'welcome [u]to[/u] [b]php freaks[/b]!'; $expressions = array( '#([[]b[]])(.*)([[]/b[]])#e', '#([[]u[]])(.*)([[]/u[]])#e' ); $results = array( '(\'<span style="font-weight:bold;">$2</span>\')', '(\'<span style="text-decoration:underline;">$2</span>\')' ); echo preg_replace($expressions,$results,$string); My New Concept $string = 'welcome [u]to[/u] [b]php freaks[/b]!'; $expressions = array( '/\[b\](.*?)\[\/b\]/', '/\[u\](.*?)\[\/u\]/' ); $results = array( '<span style=font-weight:bold;>\\1</span>', '<span style=text-decoration:underline;>\\1</span>' ); echo preg_replace($expressions,$results,$string); 1st issue is when i have multiple codes, for example [b]hi[/b], whats [b]your[/b] name bold results are [/b]hi, whats [b]your the 1st issue is solved with my new concept, however now i have an issue, my code is adding quotes for me to my html. i am not sure how to explain it. for example <span style=text-dexoration:underline;>blaa</span> gets rendered like <span style="text-dexoration:underline;">blaa</span> i will look into this, i am not sure whats causing it atm :S i need some feed back on my 2 concepts please Quote Link to comment Share on other sites More sharing options...
Adam Posted February 1, 2012 Share Posted February 1, 2012 How are you viewing the source? If it's through a tool like Firebug or Chrome's developer tools, then it probably re-formats the HTML (correctly). Why are you leaving out the quotes out of interest? Quote Link to comment Share on other sites More sharing options...
RobertP Posted February 1, 2012 Author Share Posted February 1, 2012 How are you viewing the source? If it's through a tool like Firebug or Chrome's developer tools, then it probably re-formats the HTML (correctly). Why are you leaving out the quotes out of interest? i am using chrome, and if i add the quotes like this <span style="font-weight:bold;">blaa</span> i receive this as a result <span style="\"font-weight:bold;"\">blaa</span> adds a slash, and a new quote :S Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 1, 2012 Share Posted February 1, 2012 Are you saving this to a database? Or formatting it on output? If you're saving it to a database and are using mysql_real_escape_string then you'll need to strip them off for output with stripslashes(). If you're not saving it to a database you probably have magic quotes on, so turn that off in the php.ini. Quote Link to comment Share on other sites More sharing options...
RobertP Posted February 2, 2012 Author Share Posted February 2, 2012 saving to mysql, and yes i am using stripslashes Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 2, 2012 Share Posted February 2, 2012 Have you checked if you have magic quotes on? If you do it is going to double slash it when you save it to MySQL. You can either look in the php.ini for "magic_quotes_gpc" or run this code if(get_magic_quotes_gpc()) { echo 'magic quotes are on'; } Quote Link to comment Share on other sites More sharing options...
Adam Posted February 2, 2012 Share Posted February 2, 2012 1) I wouldn't replace the BBCode before you save the data. What you have in the database should be kept as close to what the user entered as possible. What will happen when you allow the user to edit their post/input? Or what if you decide to serve the data on another platform (Android / iOS)? You should parse the BBCode and convert it to HTML as it's output to a web page. 2) On top of what scootstah suggested, can you show us the code you're using to insert / display the data? 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.