EffakT Posted February 14, 2014 Share Posted February 14, 2014 (edited) So, the problem I have is that I have this code if (strpos($content,"[ol]") !== false) { $content = str_replace("[ol]", "<ol>", $content); $content = str_replace("[/ol]", "</ol>", $content); $content = str_replace("[li]", "<li>", $content); $content = str_replace("[/li]", "</li>", $content); } if (strpos($content,"[ul]") !== false) { $content = str_replace("[ul]", "<ul>", $content); $content = str_replace("[/ul]", "</ul>", $content); $content = str_replace("[li]", "<li>", $content); $content = str_replace("[/li]", "</li>", $content); } I want it to add a <p> tag after the closing of the list, though a problem I came across was if it was a multi-dimensional list, e.g: First Dimension Second Dimension Back to First Dimension The problem is, it would add a <p> tag after the closing on the Second Dimension's closing tag... if that makes sense.This might help to explain:http://phpfiddle.org/lite/code/niw-dtgIf anybody can help, it would be greatly appreciated Thanks --EffakT Edited February 14, 2014 by EffakT Quote Link to comment Share on other sites More sharing options...
denno020 Posted February 14, 2014 Share Posted February 14, 2014 Can you show me an example outline of the code that you would like to end up with? In your PHPFiddle, the $content variable already has <p> tags.. I don't quite understand where you want them to be.. If you can show me the outline that you want to achieve, I'm sure I'll be able to help you with your loop. Denno Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 14, 2014 Author Share Posted February 14, 2014 Well, if i am correct in this, <ol> and <ul> close <p> tags when they are opened, so I need to open the <p> again after the </ol> and </ul> to keep my <p> open for the future content. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 14, 2014 Share Posted February 14, 2014 You need to use regex to clean up your html. $content = preg_replace('~(<([uo]l)>.*</\\2>)~', "</p>$1<p>", $content); The above regex will wrap </p> and <p> before/after a list. Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 14, 2014 Author Share Posted February 14, 2014 Sweet, I'm not great with regex, so it helps a lot thanks man! Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 14, 2014 Author Share Posted February 14, 2014 You need to use regex to clean up your html. $content = preg_replace('~(<([uo]l)>.*</\\2>)~', "</p>$1<p>", $content); The above regex will wrap </p> and <p> before/after a list. One problem, if the $content has multiple lines, e.g: <?php $content = "<p>Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3</p>"; if (strpos($content,"[ol]") !== false) { $content = str_replace("[ol]", "<ol>", $content); $content = str_replace("[/ol]", "</ol>", $content); $content = str_replace("[li]", "<li>", $content); $content = str_replace("[/li]", "</li>", $content); } if (strpos($content,"[ul]") !== false) { $content = str_replace("[ul]", "<ul>", $content); $content = str_replace("[/ul]", "</ul>", $content); $content = str_replace("[li]", "<li>", $content); $content = str_replace("[/li]", "</li>", $content); } $content = preg_replace('~(<([uo]l)>.*</\\2>)~', "</p>$1<p>", $content); echo $content; ?> It will fail. I'd rather not have to clear all of the \n from my content when retrieved from the database. Any idea? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 14, 2014 Share Posted February 14, 2014 Change '~(<([uo]l)>.*</\\2>)~' to '~(<([uo]l)>.*</\\2>)~s' Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 15, 2014 Author Share Posted February 15, 2014 Problemhttp://phpfiddle.org/lite/code/j47-2ga The content between the two lists doesn't get enclosed in <p>. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 15, 2014 Share Posted February 15, 2014 Which one? <ol> <li> First Dimension <ol> <li>Second Dimension</li> </ol> </li> <li>Back to First Dimension</li> </ol> Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 15, 2014 Author Share Posted February 15, 2014 Which one? <ol> <li> First Dimension <ol> <li>Second Dimension</li> </ol> </li> <li>Back to First Dimension</li> </ol> <p>Content Content Content </p><ol> <li>First Dimension <ol> <li>Second Dimension</li> </ol> </li> <li>Back to First Dimension</li> </ol> More Content :3 <ol> <li>First Dimension <ol> <li>Second Dimension</li> </ol> </li> <li>Back to First Dimension</li> </ol><p> More Content :3</p> line 10. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 15, 2014 Share Posted February 15, 2014 What's the last/updated php script what generates this output? Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 15, 2014 Author Share Posted February 15, 2014 <?php $content = "<p>Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3</p>"; if (strpos($content,"[ol]") !== false) { $content = str_replace("[ol]", "<ol>", $content); $content = str_replace("[/ol]", "</ol>", $content); $content = str_replace("[li]", "<li>", $content); $content = str_replace("[/li]", "</li>", $content); } if (strpos($content,"[ul]") !== false) { $content = str_replace("[ul]", "<ul>", $content); $content = str_replace("[/ul]", "</ul>", $content); $content = str_replace("[li]", "<li>", $content); $content = str_replace("[/li]", "</li>", $content); } $content = preg_replace('~(<([uo]l)>.*</\\2>)~s', "</p>$1<p>", $content); echo $content; ?> Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 15, 2014 Share Posted February 15, 2014 What about this: <?php $content = "<p>Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3</p>"; $p1 = '~\[\/ol\](\s\n)(.*)(\s\n)\[ol\]~'; $r1 = '<p>$2</p>'; $content = preg_replace($p1, $r1, $content); $p2 = '~(\[)([a-z/]+)(\])~'; $r2 = '<$2>'; $replace_content = preg_replace($p2, $r2, $content); echo $replace_content; Result: <p>Content Content Content<ol><li>First Dimension<ol><li>Second Dimension</li></ol></li><li>Back to First Dimension</li><p>More Content :3</p><li>First Dimension<ol><li>Second Dimension</li></ol></li><li>Back to First Dimension</li></ol>More Content :3</p> Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 15, 2014 Author Share Posted February 15, 2014 What about this: <?php $content = "<p>Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3</p>"; $p1 = '~\[\/ol\](\s\n)(.*)(\s\n)\[ol\]~'; $r1 = '<p>$2</p>'; $content = preg_replace($p1, $r1, $content); $p2 = '~(\[)([a-z/]+)(\])~'; $r2 = '<$2>'; $replace_content = preg_replace($p2, $r2, $content); echo $replace_content; Result: The first paragraph doesn't get closed and the last paragraph doesn't get opened. :/ Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 16, 2014 Share Posted February 16, 2014 (edited) In that case, you should match the string until "More Content :3", after that just make copy/paste. I've trimed the code from white spaces (\s) only newlines (\n) are available. If you are on windows environment, I think you should use (\r) instead (\n) but not sure b/s don't have a copy of windows to do the test. <?php $content = "<p>Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3</p>"; $p1 = '~(<p>[a-zA-Z ]+)\n(\[ol\]\n\[li\][a-zA-Z ]+\n\[ol\]\n\[li\][a-zA-Z ]+\[\/li\]' . '\n\[\/ol\]\n\[\/li\]\n\[li\][a-zA-Z ]+\[/li\]\n\[\/ol\])\n(.*)\n' . '(\[ol\]\n\[li\][a-zA-Z ]+\n\[ol\]\n\[li\][a-zA-Z ]+\[\/li\]' . '\n\[\/ol\]\n\[\/li\]\n\[li\][a-zA-Z ]+\[/li\]\n\[\/ol\])~'; $r1 = '$1</p>$2<p>$3</p>$4<p>'; $content = preg_replace($p1, $r1, $content); $p2 = '~(\[)([a-z/]+)(\])~'; $r2 = '<$2>'; $replace_content = preg_replace($p2, $r2, $content); echo $replace_content; Result: <p>Content Content Content</p><ol><li>First Dimension<ol><li>Second Dimension</li></ol></li><li>Back to First Dimension</li></ol><p>More Content :3</p><ol><li>First Dimension<ol><li>Second Dimension</li></ol></li><li>Back to First Dimension</li></ol><p>More Content :3</p> PS: The pattern ($p1) looks complicated but actualy is very simple. Edited February 16, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 16, 2014 Author Share Posted February 16, 2014 could you by change explain what $p1 matches/does? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 16, 2014 Share Posted February 16, 2014 It's a simple regular expression. The best RegExp resource I know about is Regular-Expressions. Definitely worth a read! Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 16, 2014 Author Share Posted February 16, 2014 What regex should be used to clear the whitespace? e.g when getting the content from database, wouldn't It need to clear all of the whitespace for that regex to work? Quote Link to comment Share on other sites More sharing options...
mogosselin Posted February 16, 2014 Share Posted February 16, 2014 You can easily strip all the line breaks with this piece of code: $output = "abc def hij"; echo preg_replace('/^\s+|\n|\r|\s+$/m', '', $output); Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 16, 2014 Share Posted February 16, 2014 Any reason why, do you want to insert html markup in to the database? Why don't you start to tell us the whole story? Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 16, 2014 Author Share Posted February 16, 2014 Setting up a blog, and I want to be able to insert lists, urls, bold, italics, etc, etc, using bbcode. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 16, 2014 Share Posted February 16, 2014 (edited) Well, if the format is always like this: $content = "<p>Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3</p>"; you would use a trim with character_mask parameter = '' to trim only the white spaces before to insert a content to database. There is a couple of other options to trim/remove them of course. Edited February 16, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 16, 2014 Author Share Posted February 16, 2014 the database content looks like this: Content Content Content[ol][li]First Dimension[ol][li]Second Dimension[/li][/ol][/li][li]Back to First Dimension[/li][/ol]More Content :3[ol][li]First Dimension[ol][li]Second Dimension[/li][/ol][/li][li]Back to First Dimension[/li][/ol]More Content :3 It is then called from the database. Regex is run to format it into html it is then echoed like: <p><?php $content ?></p> Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 16, 2014 Share Posted February 16, 2014 (edited) Something like this: $content = " Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension [/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 "; // before replacement echo strlen($content).'<br />'; // 298 $str_r = str_replace(' ','',$content); //after replacement echo strlen($str_r); //253 Final result: ContentContentContent[ol][li]FirstDimension[ol][li]SecondDimension[/li][/ol][/li][li]BacktoFirstDimension[/li][/ol]MoreContent:3[ol][li]FirstDimension[ol][li]SecondDimension[/li][/ol][/li][li]BacktoFirstDimension[/li][/ol]MoreContent:3 Edited February 16, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
EffakT Posted February 16, 2014 Author Share Posted February 16, 2014 Something like this: $content = " Content Content Content [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension [/li] [/ol] More Content :3 [ol] [li]First Dimension [ol] [li]Second Dimension[/li] [/ol] [/li] [li]Back to First Dimension[/li] [/ol] More Content :3 "; // before replacement echo strlen($content).'<br />'; // 298 $str_r = str_replace(' ','',$content); //after replacement echo strlen($str_r); //253 Final result: We don't want the user's post to be cleared of spaces, just need to clear the white-space so the regex will work. 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.