Marcello Impastato
Marcello Impastato

Reputation: 2281

how replace a tag with attribute to string empty?

How i can replace a string from:

"this is a <p id="1" /> text <p id="2" /> of a string"

To:

"this is a <br> text <br> of a string" ?

Shortly i want replace all <p ... /> present in a string with <br>.

Upvotes: 0

Views: 44

Answers (3)

ADev
ADev

Reputation: 686

update: (just <p ... />)

echo preg_replace("/<p[^\/]*\/>/i", "<br />", 'this is a <p id="1" /> text <p id="2" /> of a string');

Upvotes: 2

user1134181
user1134181

Reputation:

Try it:

<?php
echo preg_replace("<p id=\"d\"/>", "<br>", "this is a <p id=\"1\" /> text <p id=\"2\" /> of a string");
?>

You'll get:

this is a

text

of a string

Upvotes: 0

devpro
devpro

Reputation: 16117

You can use preg_replace() along with str_replace()

$newString = preg_replace("/<p[^>]*?>/", "", $yourString); // will remove all starting tag
$addNewLine = str_replace("</p>", "<br />", $newString); // will replace closing tag with <br>

Upvotes: 0

Related Questions