How can I make sure that redundant <p> tags do not violate the style?

I get an unreliable result from the Tumblr API. When I get one line of text, I get only text. When there are multiple lines, each line is wrapped with a tag <p>. So I either get:

This is a value with no second line

or

<p>This is a value</p>
<p>with a second line</p>

I want both displayed the same way. I manually added <p>around the value, but this will result in:

<p><p>This is a value</p>
<p>with a second line</p></p>

for multi-line values.

I have a few thoughts on how to fix this, but I'm not sure if it would be better.

  • Create a new CSS style p p {}that shifts the style of the twisted style
  • Use jQuery to retrieve DOM contents of redundant tags <p>and make them non-redundant
  • jQuery, , <p>, , , DOM.

, 1 ( ), . , 2 3, ?

: , Tumblr, Python/PHP/ASP/etc ( !).

+3
5

:

result = /^\s*\<p\>.+\<\/p\>$/.test(result) ? result : '<p>' + result + '</p>';

, .

+2

if ( str.indexOf('<p>') === -1 ) {
   str = '<p>' + str + '</p>';    
}

, "<p>", , "", "<p>".

+4

, , 3 - . , :

, (php/asp.net/etc..), , , .

  • (1 )
  • split
  • <p>,
  • <p> </p>
  • dom

Viola! 1 10 , <p> 10 <p>, .

"" , , , apis api

+1
source

Assuming PHP; and if the string format is consistent ~

$str = 'hello world';
//$str = '<p>a string with a p tag</p>';

if(substr($str,0,3)=='<p>') {

    echo $str;

} else {

    echo '<p> ' . $str . '</p>';

}

// <p>hello world</p>
// <p>a string with a p tag</p>
0
source

Another solution would be to replace all /<\/p>[^<]*<p>/gwith <br/>, which will keep line breaks in multi-line paragraphs.

0
source

All Articles