1

I have a weird issue with my html codes.

I want to add the a variable $htmlstring to a <p> tag.

I do

 $finalHtml .= '<p class="text">'.$htmlString.'</p>';

My $htmlstring contains something like

<span>texts….</span>
<span>texts….</span>
<span>texts….</span>
<span>texts….</span>
<span>texts….</span>

<table>
    <tr>
       <td>cell</td>
      <td>cell</td>
      <td>cell</td>
     </tr>
    more….
</table>

<span>more texts</span>
<span>more texts</span>

I want my result to become like

<p class='text'>
     <span>texts….</span>
    <span>texts….</span>
    <span>texts….</span>
    <span>texts….</span>
   <span>texts….</span>

   <table>
     <tr>
        <td>cell</td>
       <td>cell</td>
       <td>cell</td>
      </tr>
      more….
   </table>
   <span>more texts</span>
   <span>more texts</span>
</p>

but i got

//p tag only wraps texts before my table element.
<p class='text'>
     <span>texts….</span>
    <span>texts….</span>
    <span>texts….</span>
    <span>texts….</span>
   <span>texts….</span>
</p>

   <table>
     <tr>
        <td>cell</td>
       <td>cell</td>
       <td>cell</td>
      </tr>
      more….
   </table>
   <span>more texts</span>
   <span>more texts</span>

can anyone help me with this weird issue? Thanks a lot!

4
  • <p> cannot contain a <table>, that isn't valid HTML. Commented Sep 6, 2013 at 20:07
  • check this out: table-tag-not-nesting-inside-p-tags-in-dom Commented Sep 6, 2013 at 20:08
  • 3
    Have you taken a look at the raw output of the php, before the browser interprets it? I suspect your p end tag appears where you wanted it, but the browser reinterpreted the page. Commented Sep 6, 2013 at 20:08
  • $finalHtml .= "<div class=\"text\">${htmlString}</div>"; Commented Sep 6, 2013 at 20:08

2 Answers 2

6

That's because p can't hold a block element (table).

Use a div instead of your paragraph.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried running your code through the validator?

the validator would have told you that you cannot put

 <p> <table> ...

or

<table> <p> 

check out: http://validator.w3.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.