SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unnecessary <p></p> tags help?

    Hi All,

    I was wondering if anyone could help. I have a column in my database which contains a story. Now I want to take the first paragraph, and make it bold (which works!) and then with the rest of the paragraphs I just want a <p> tag around each line, but my output produces two unnecessary <p></p> tags (as shown below) with each new line.


    <p style="font-weight:bold">Open 24 hours a day, 365 days a year.
    </p><p>
    </p><p>lipsum text goes here
    </p><p>
    </p><p>And here is some more text that goes here and it will go here in this space here provided.
    </p><p>
    Here’s my code, can anyone advise how I can rectify this problem?

    PHP Code:
    <?
    $aParagraphs 
    explode("\n"$about); 
    echo 
    '<p style="font-weight:bold">' htmlspecialchars_decode($aParagraphs[0]) . '</p>';
    for (
    $i=1$i count($aParagraphs); $i++) {
       echo 
    '<p>' htmlspecialchars_decode($aParagraphs[$i]) . '</p>';
    }  
    ?>
    Thanks

  2. #2
    SitePoint Zealot smadeira's Avatar
    Join Date
    Oct 2003
    Location
    Pennsylvania
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works when I run it on my system. What is the contents of $about? If there are multiple \n (for instance it is double spaced) you would get the extra <p></p>. I used this string:

    PHP Code:
    $about"Open 24 hours a day, 365 days a year. \n lipsum text goes here \n And here is some more text that goes here and it will go here in this space here provided."
    Scott

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either use trim() and then test that there is really something there other than whitespace, or use a regular expression to split on multiple newlines.
    PHP Code:
    $aParagraphs preg_split('#(\r\n|\r|\n)+#'$about); 
    You might also try a web search on nl2p(), a common user defined function which uses <br> for single newlines, and wraps multiple newlines in a new set of <p> tags.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If agree with crmmalibu. It seems that there are additional carriage returns that are causing the problem. Try the following:

    PHP Code:
    $aParagraphs explode("\n"$about); 
    echo 
    '<p style="font-weight:bold">' htmlspecialchars_decode($aParagraphs[0]) . '</p>'
    for (
    $i=1$i count($aParagraphs); $i++) {    
       if (
    trim($aParagraphs[$i]) != "") {
          echo 
    '<p>' htmlspecialchars_decode($aParagraphs[$i]) . '</p>';
       }



Bookmarks

Posting Permissions

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