View Full Version : Making a smarter paragraph former?

03-26-2005, 08:41 AM
Currently, I have a basic nl2p function it wraps the text in a <p> tag and then, for every \n it replaces with a </p><p> with an optional CSS class on the opening. However, if you examine this:

However, then I end up with something like this....

<p>Grab open the door</p>
<p>I can actually see my breath tonight</p>
<p>That doesnt mean im breathing</p>
<p>Crack the smile just for the stake of me</p>
<p>This could take a while when im loud</p>

<p>Silence is golden especially in this case</p>
<p>I'm not too sure that I want it to be this way</p>
<p>Open mouth closed eyes</p>
<p>No ones are escaping</p>
<p>It's our nerve</p>
<p>It's to die to see were inventing</p>
<p>The way it all fits together at night</p>
<p>Together at night</p>
<p>I think it's time to turn around</p>

<p>I really wanted you all tonight</p>
<p>I think it's... <a href="#" class="more">Read More</a></p>

Not exactly what I'd want. Not to mention that there will eventually be code included into this project (by that I mean, along the lines of ALA). And speaking of ALA, that's more what I am aiming for, but would it be easier for me to code in the HTML by hand (e.g. add every paragraph, etc, etc)?

Current PHP Code:

function nl2p($text, $class='')
$class = ( ($class != '') ? ' class="' . $class . '"' : '');

$text = '<p' . $class . '>' . $text . '</p>';
$text = str_replace("\n",'</p><p' . $class . '>',$text);
$text = str_replace("\r",'',$text);
$text = str_replace('<p' . $class . '></p>','',$text);
$text = str_replace('</p><p' . $class . '>', "</p>\n<p". $class . ">", $text);
return $text;

03-26-2005, 04:36 PM
Not exactly what I'd want.
What do you want? :)

03-26-2005, 05:50 PM
I don't want a paragraph formed on every new line, especially if I am going to be adding something along the lines of BB code. Maybe ALA was a bad example...something like a forum is a better example. Take here for instance.

I can type this.


And then maybe an image and a link, the whole nine yards, but there aren't breaks placed at the end of whatever list the [list]
tag is made into (<ol> I think >_>) and there aren't breaks after of the </li>s (At least I hope not, then my point is moot even though they shouldn't be there in the first place).

Is that a better example?

03-26-2005, 06:55 PM
Ok so you want to do something like bbcode? I still don't know what you want to do with the <p> tag. How the tags are displayed is rather browser depending not php.

03-26-2005, 08:49 PM
I was explaining it poorly, but I found a solution here (http://smarty.incutio.com/?page=ParagraphPlugin) . =D