Jump to content

PHP Auto Insert <p> tags after lists


EffakT

Recommended Posts

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:

  1. First Dimension
    1. Second Dimension
  2. 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-dtg
If anybody can help, it would be greatly appreciated smile.png

Thanks

 

 

--EffakT

Edited by EffakT
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites


<?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;
?>
Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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. :/

Link to comment
Share on other sites

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 by jazzman1
Link to comment
Share on other sites

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 by jazzman1
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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 by jazzman1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.