0

I got a *.txt file that contains html code. I want to remove:

<textarea cols="100" rows="50" name="newcontent" id="newcontent">

and

</textarea>

I'm able to remove first tag with this:

$content_replaced = preg_replace ("/<textarea cols=\"100\" rows=\"50\" name=\"newcontent\" id=\"newcontent\">/", "", $replace);

but can't figure out how to replace < /textarea> aswell. Thanks :)

Kris
  • 1,067
  • 2
  • 12
  • 15
  • So you can replace `a` with nothing but cannot replace `b` with nothing? Because in that position I would first think about doing with `b` the same as I did with `a`. – Jon Aug 27 '13 at 19:41
  • 1
    You don't use regexes on HTML. Use DOM, and removal of a tag/element becomes trivial. – Marc B Aug 27 '13 at 19:45
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Philip Whitehouse Aug 27 '13 at 20:00

2 Answers2

0

Try:

$content_replaced = preg_replace ("/<textarea cols=\"100\" rows=\"50\" name=\"newcontent\" id=\"newcontent\">.*?<\/textarea>/", "", $replace);

Basically, I just added .*? to your regexp. What that will do is it will "non-greedily" match all content up until the next closing tag.

user2588667
  • 480
  • 3
  • 10
0

You can try this regexp. Here is example.

$string = "sdfsd <textarea cols=\"100\" rows=\"50\" name=\"newcontent\" id=\"newcontent\"> some text here </textarea>";
$result  = preg_replace("/.*?>(.*)<.*?>$/","$1",$string);
echo $result;

This will work only when You have only one tag in line.

Valery Statichny
  • 593
  • 7
  • 22