Reputation: 2281
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
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
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
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