0

hello i have an html string with output

$incoming_data = '<div id=\"title\">a title</div>';

And i want to remove it with preg_replace

i tried the code bellow without any luck..

result = preg_replace('#<div id="title">(.*?)</div>#', ' ', $incoming_data);

Any ideas?

Irene T.
  • 1,393
  • 2
  • 20
  • 40
  • 2
    Any ideas? Yep, show the actual HTML source (without escaped quotes for example). And when unversed with regex, use a DOM traversal frontend for modifying HTML. (Except if it's really for repetitive templating tasks of course.) – mario Oct 24 '15 at 13:35
  • Also, be sure you are doing `$result =`, with the `$`. – elixenide Oct 24 '15 at 13:43
  • Any help how to do this with DOM travesal? – Irene T. Oct 24 '15 at 18:20

1 Answers1

2

The variable $incoming_data contains escaped quotes, so you have to escape the backslash for the php regex pattern to match it.

Then your updated code would be:

$incoming_data = '<div id=\"title\">a title</div>';
$result = preg_replace('#<div id=\\\"title\\\">(.*?)</div>#', ' ', $incoming_data);

If you first want to strip the slashes from the string, you can use the stripslashes function.

Then your updated code would be:

$incoming_data = stripslashes('<div id=\"title\">a title</div>');
$result = preg_replace('#<div id="title">(.*?)</div>#', ' ', $incoming_data);

For the dom traversal you can use the DOMDocument class.

Community
  • 1
  • 1
The fourth bird
  • 154,723
  • 16
  • 55
  • 70