Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    mypointofview
    Guest

    Empty <p></p> how to get rid of

    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") 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 but it did not work - perhaps I used it wrongly. On the other hand I read that it's a bug 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, a Macintosh application) using LF line feeds (my server is Apache) which I include in a page:

    PHP Code:
    <?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.
    Last edited by mypointofview; 07-23-2007 at 04:31 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Any empty paragraph is okay but its just not necessary. I would use a preg_replace on the file before you echo it.
    PHP Code:
    <?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";

    }  

    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #4
    mypointofview
    Guest
    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:

    Code:
    <p>
    </p>
    Unfortunately I don't understand the string that AeroSpace searches and replaces (=eliminates)...

    Maybe it just needs a bit tweaking ?

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Can you post an example of the the string that needs to be replaced?
    Try this
    PHP Code:
    <?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.fil...t-line-endings
    Last edited by _Aerospace_Eng_; 07-23-2007 at 08:27 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    mypointofview
    Guest
    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:

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

    PHP Code:
    <!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:

    PHP Code:
    <!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 returnsThe text file here was created using LF line endings.</p>

    </
    body>
    </
    html
    The goal is to get rid of the

    Code:
    <p>
    </p>
    Last edited by mypointofview; 07-23-2007 at 09:49 PM.

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this
    PHP Code:
    <?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";
        }
    }  
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    mypointofview
    Guest
    Aerospace, you're a genius It does exactly what was needed. the output is now:

    Code:
    <!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 !

  • #9
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I'm curious since I don't have a Mac would this also work?
    PHP Code:
    <?php 
    error_reporting
    (E_ALL);
    $arr_content file('text.txt');
    foreach(
    $arr_content as $p)
    {
        if(
    $p != "\n")
        {
            echo 
    '<p>'.trim($p)."</p>\n";
        }
    }  
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #10
    mypointofview
    Guest

    Yes !

    Works as well - more elegant. Super!

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

  • #11
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •