...

View Full Version : Empty <p></p> how to get rid of



mypointofview
07-23-2007, 03:27 AM
Hello, I'm setting up a home made Content Management System, running a loop that outputs every line of an external text file with <p> in front and </p> at the end. If a line is empty it outputs <p></p>. An empty line is created each time I hit the return key twice.

My validator in Safari ("SafariTidy (http://zappatic.net/safaritidy/)") complains and gives warning about inaccurate code, saying an empty <p> is not allowed. I'm using html 4.01 strict as DTD.

Questions:

1. Should I just ignore the warning? (feels bad to not see the green check mark anymore)

2. Is there a way to skip empty lines? I tried FILE_SKIP_EMPTY_LINES (http://www.php.net/file) but it did not work - perhaps I used it wrongly. On the other hand I read that it's a bug (http://www.mail-archive.com/php-bugs@lists.php.net/msg97959.html) in PHP... I'm testing all locally running PHP 5.22 on Macintosh Tiger 10.4.10.

3. Is there a way to erase the string <p></p> before writing the loop to the output file?

I'm using a plain text file (created by TextMate (http://www.macromates.com/), a Macintosh application) using LF line feeds (my server is Apache) which I include in a page:


<?php

error_reporting(E_ALL);

$arr_content = file('text.txt', FILE_SKIP_EMPTY_LINES);

foreach($arr_content as $p) {

echo '<p>'.$p."</p>\n";

}

?>

Thank you for any input to this topic.

_Aerospace_Eng_
07-23-2007, 04:15 AM
Any empty paragraph is okay but its just not necessary. I would use a preg_replace on the file before you echo it.

<?php

error_reporting(E_ALL);

$arr_content = file('text.txt', FILE_SKIP_EMPTY_LINES);

foreach($arr_content as $p) {
$p = preg_replace("/<(p).*?><(\/p)>/xmsi",'',$p);
echo '<p>'.$p."</p>\n";

}

?>

whizard
07-23-2007, 04:49 AM
If I understand your problem fully, it seems that you could just do this:

foreach($arr_content as $p) {
if(strlen($p) > 0)
{
echo '<p>'.$p."</p>\n";
}
}

HTH
Dan

mypointofview
07-23-2007, 06:58 AM
Does not work :(

From both solutions, I think (with my limited knowledge) that the way to search and replace seems the most usable.

The empty line characters (the empty "p") is output in the final html file underneath each other, like so:


<p>
</p>

Unfortunately I don't understand the string that AeroSpace searches and replaces (=eliminates)...

Maybe it just needs a bit tweaking ?

_Aerospace_Eng_
07-23-2007, 07:14 AM
Can you post an example of the the string that needs to be replaced?
Try this

<?php

error_reporting(E_ALL);

$arr_content = file('text.txt', FILE_SKIP_EMPTY_LINES);

foreach($arr_content as $p) {
$ismatch = preg_match('/[\s,]+/xmsi',$p); // looks for any whitespace such as spaces, \t, \n, \r, and \f
if($ismatch == 0) // preg_match will return 0 if it doesn't find any patterns matching the regex, if its false or 0 then we can be sure that $p does not contain whitespace
{
echo '<p>'.$p."</p>\n";
}
}

?>
There seems to be issues with FILE_IGNORE_EMPTY_LINES. You may have to enable the option discussed here: http://us2.php.net/manual/en/ref.filesystem.php#ini.auto-detect-line-endings

mypointofview
07-23-2007, 08:37 PM
Unfortunately this time the page is blank (nothing in body tag).

I set the flag on my PHP 5.22 installation for auto_detect_line_endings to 1, rebooted the server, verified and it's the same like as if it's set to 0.

Here's the content of the plain text file which I'd like to use:


First line with some European characters:

Now another line after two returns. The text file here was created using LF line endings.

P.S. I like to use <p> and not <br> because the p can be styled...

With my initial php file...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>test 10</title>
</head>
<body>

<?php

error_reporting(E_ALL);

$arr_content = file('test.txt');

foreach($arr_content as $p) {

echo '<p>'.$p."</p>\n";

}

?>

</body>
</html>


... I obtain this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>test 10</title>
</head>
<body>

<p>First line with some european characters:
</p>
<p>
</p>
<p>Now another line after two returns. The text file here was created using LF line endings.</p>

</body>
</html>

The goal is to get rid of the


<p>
</p>

:confused:

_Aerospace_Eng_
07-23-2007, 09:15 PM
Try this

<?php
error_reporting(E_ALL);
$arr_content = file('text.txt');
foreach($arr_content as $p)
{
$ismatch = preg_match('/^[\s,]+$/',$p); // looks for any whitespace such as spaces, \t, \n, \r, and \f
if($ismatch != 1)
{
echo '<p>'.trim($p)."</p>\n";
}
}
?>

mypointofview
07-23-2007, 09:47 PM
Aerospace, you're a genius :thumbsup: It does exactly what was needed. the output is now:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>test 102</title>
</head>
<body>

<p>First line with some european characters: 123</p>
<p>Now another line after two returns. The text file here was created using LF line endings.</p>


</body>

</html>

Thank you !

_Aerospace_Eng_
07-23-2007, 09:51 PM
I'm curious since I don't have a Mac would this also work?

<?php
error_reporting(E_ALL);
$arr_content = file('text.txt');
foreach($arr_content as $p)
{
if($p != "\n")
{
echo '<p>'.trim($p)."</p>\n";
}
}
?>

mypointofview
07-25-2007, 10:43 PM
Works as well - more elegant. Super! :thumbsup:

Tested locally on Macintosh:
- Tiger 10.4.10
- PHP 5.22
- auto_detect_line_endings set to 1 (probably not necessary)
- Firefox and Safari

_Aerospace_Eng_
07-26-2007, 08:00 AM
Cool. The difference between the two is the first one checks for any whitespace where as the second only checks for the \n character though in a text file this usually is the only white space character for a new line.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum