1

Im trying to remove some '<p>' tags from a string returned by wordpress. I've had a look at the php documentation but I can't seem to find a function that will alow me to define a string and then strip that string from a variable. Im new to php and I realize this must be a pretty simple operation. Any suggestions?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Thomas
  • 5,030
  • 20
  • 67
  • 100
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon May 10 '11 at 15:01
  • Please clarify whether you just want to remove the P tags or also the contents of the P elements. In addition, clarify if the P elements can occur with any attributes. Also point out whether your real goal is to remove all or just some elements, e.g. does your content only contain p elements but you really want to remove any elements or do you want to selectively remove elements, e.g. remove p elements but keep others. – Gordon May 10 '11 at 15:06
  • I just would like to remove the

    tags.

    – Thomas May 10 '11 at 15:53
  • so there is other elements in the markup? You want to remove the P tags, keep the content and also keep any other elements and their content? – Gordon May 10 '11 at 17:16

5 Answers5

2

you could use str_replace http://www.php.net/manual/en/function.str-replace.php

peaceman
  • 2,549
  • 19
  • 14
1
strip_tags($mystring); // this will remove all tags
Headshota
  • 21,021
  • 11
  • 61
  • 82
1

Try this:

$myString = "<p>whatever you want in your string</p>";
$newString = str_replace(["<p>","</p>"], "", $myString);
echo $newString;

Explanation

Using str_replace, you can replace one or more strings with one or more other strings. In this case, we're replacing any <p> and </p> tags with an empty string.

Dave
  • 28,833
  • 23
  • 113
  • 183
0

strip_tags() might help you.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

I think you're looking for either of str_replace() or (more likely, since you only want to replace some of them) preg_match().

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154