0

str_ireplace with replace ends my html tag.

            $txt = '<p><strong>Add on </strong>[number],<strong> number nr </strong></p>';
            $htmlValue = '<div class="resizing-input"><input type="text" value="1"><span style="display:none"></span></div>';
            $text = str_ireplace('[number]', $htmlValue, $txt);//It needs to be case-insensitive so str_ireplace not str_replace.
            echo'<pre>';print_r($text);echo'</pre>';die;

            //return: <pre><p><strong>Add on </strong></p><div class="resizing-input"><input type="text" value="1"><span style="display:none"></span></div>,<strong> number nr </strong><p></p></pre>

            //should return: <pre><p><strong>Add on </strong><div class="resizing-input"><input type="text" value="1"><span style="display:none"></span></div>,<strong> number nr </strong></p></pre>

Why it does like that?
I want to replace string between [] to html but it ends another html </p> tag. Same thing happens with str_replace too.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
user2809327
  • 147
  • 1
  • 11
  • The php functions certainly do _not_ add arbitrary html tags. Most likely you check the result in your browser and that tries some form of automatic correction of an invalid html structure you output. Browsers do that. – arkascha Mar 06 '16 at 17:59
  • [Looks like it works correctly to me](https://3v4l.org/RMvLN) – Mark Baker Mar 06 '16 at 18:00

1 Answers1

0

The thing is I can't write <div> tag inside a <p> element.
Syntax of the p element does not allow a div child, and the end tag </p> may be omitted, the validator (and a browser) implies </p> when it encounters a <div> tag when parsing a p element. That is, when p is being parsed (or “is open”), the start tag of a div element implicitly closes it.

No p element in scope but a p end tag seen.w3c validation

Community
  • 1
  • 1
user2809327
  • 147
  • 1
  • 11