user1128331
user1128331

Reputation: 727

php: replace tag html

I would like to replace tag html

<p>Lundium purus ac nec! Enim porta cras! Elementum adipiscing vel mauris lundium! Et platea porta! Enim auctor nec enim enim nisi vel aliquet, odio! Mid. Facilisis.</p>
<p class="class_no">Lundium purus ac nec! Enim porta cras! Elementum adipiscing vel mauris lundium! Et platea porta! Enim auctor nec enim enim nisi vel aliquet, odio! Mid. Facilisis.</p>

to be like this

Lundium purus ac nec! Enim porta cras! Elementum adipiscing vel mauris lundium! Et platea porta! Enim auctor nec enim enim nisi vel aliquet, odio! Mid. Facilisis.<br/>
Lundium purus ac nec! Enim porta cras! Elementum adipiscing vel mauris lundium! Et platea porta! Enim auctor nec enim enim nisi vel aliquet, odio! Mid. Facilisis.<br/>

Is it possible?

Edit: I would like to replace all

<p *></p>

to be

<br />

Upvotes: 0

Views: 118

Answers (4)

alwaysLearn
alwaysLearn

Reputation: 6950

If this html is in string then you can use preg_replace

<?php
$string = '<p>Lundium purus ac nec! Enim porta cras! Elementum adipiscing vel mauris lundium! Et platea porta! Enim auctor nec enim enim nisi vel aliquet, odio! Mid. Facilisis.</p>
<p class="class_no">Lundium purus ac nec! Enim porta cras! Elementum adipiscing vel mauris lundium! Et platea porta! Enim auctor nec enim enim nisi vel aliquet, odio! Mid. Facilisis.</p>';

$to_be_replaced = array('%\<p(.)*?\>%', '%\</p\>%');
$replace    = array('',"<br/>");

echo preg_replace($to_be_replaced ,$replace, $string);

?>

Upvotes: 0

aman
aman

Reputation: 97

You can use strip_tags($string) function for prevent html tags.

Upvotes: 0

ITChristian
ITChristian

Reputation: 11271

You can use also, JavaScript:

var spans = $('p');

spans.contents().unwrap();

Upvotes: 2

Rob W
Rob W

Reputation: 9142

Regex is not for HTML; but you can use PHP's function strip_tags, see: https://www.php.net/strip_tags - you can also specify tags to allow.

Upvotes: 1

Related Questions