Dan Hastings
Dan Hastings

Reputation: 3290

Replacing A String With a HTML Tag Using PHP

Im reading in a piece of HTML text. I want to remove all HTML tags except paragraphs and headings. To do this i use str_replace to replace the tags that i want with string placeholders. Then strip the HTML tags. Then finally replace the string placeholders with the original HTML code. This is where it is failing.

$Text = 'ManyENH3 different';

$updatedText = str_replace("ENH3", "</h3>", $Text);

The above code wont remove the ENH3 string. I have tried messing around and it doesnt work when there is no space before or after the word. I tried using preg_replace and it returns a blank string.

Upvotes: 0

Views: 362

Answers (1)

Benjamin Poignant
Benjamin Poignant

Reputation: 1064

You can try :

$updatedText = strip_tags($Text, '<p><h1><h2><h3><h4><h5><h6>');

Upvotes: 3

Related Questions