0

I have a php file with:

$content = str_replace('search old string','by new string',$content);
$content = str_replace('search old string','by new string',$content);

It works for for search and replace or remove a string. But If old string is a paragraph, example:

<table class="adverting_I_want_to_remove">
  <tr>
    <td>Dynamic Advertise content 1</td>
    <td>Dynamic Advertise content 2</td>
  </tr>
</table>

How to remove that table in content?

Miss Phuong
  • 289
  • 1
  • 4
  • 15

3 Answers3

3
$content = str_replace('<table>
  <tr>
    <td>TD 1</td>
    <td>TD 2</td>
  </tr>
</table>','by new string',$content);

Note that if you want to replace such a piece of HTML, you must make sure you don't include extra whitespace in the string. str_replace makes an exact match and will fail otherwise.

The alternatives are

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • GolezTrol, it is not work. I tried before add question. If "old string" with break line, it will don't work – Miss Phuong Oct 18 '12 at 19:49
  • What do you mean 'don't work'? PHP does accept line breaks in strings, but like I said, str_replace does an exact match. That even means, that if you save your PHP file in Windows, you will probably have `\n\r` line endings, while the content you receive may have just `\n` line endings, or vice versa. That's why a DOM parser is the better option in this case. – GolezTrol Oct 18 '12 at 19:52
0

First off, don't use regular expressions. Your best bet to edit HTML would be to use the DOMDocument class to parse through it.

Community
  • 1
  • 1
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
-2

You can use preg_replace(). The high likelihood of str_replace() failing if/when you mistype something is why I wouldn't use it, but if you know exactly what you want to match and can type it precisely, it's the better option.

preg_replace('/<table>.*<\/table>/s', '', $content);

If you don't know how to use regular expressions, look them up and learn 'em. You can be more or less specific about what you want to replace by adjusting the regex.

user428517
  • 4,132
  • 1
  • 22
  • 39
  • the dot should come before the asterisk, and, if memory serves, the dot does not match newline characters, which would be a probelm if his html is formatted as it appears above. – Brian Warshaw Oct 18 '12 at 19:40
  • Oops, my bad. Let me defend my answer though: of course I agree that using `str_replace()` is always the better option when you don't need a regular expression (which he doesn't here), but this looks like the sort of one-off thing where rather than typing the search string out _just right_, I'd just use a quick `preg_replace()`, which would work just as well. Anyway, the other answers are better and you should listen to them. – user428517 Oct 18 '12 at 19:46
  • @LoveKen Do you have tables later in the document, or closing table tags later in the document? If so, update the expression to: `/.*?<\/table>/s` to make the wildcard/quantifier non-greedy.
    – Brian Warshaw Oct 19 '12 at 11:15