Mokhtarabadi
Mokhtarabadi

Reputation: 351

Error using preg_replace function

I want to replace only

<span class="google-src-text" style="direction: ltr; text-align: left">any character</span>

line by line with space in this source http://persianfox.ir/html.html and my php code is

$content = file_get_contents('path/to/html.html');
$content = str_replace('>', ">\n", $content);

echo preg_replace('/<span class="google-src-text" style="direction: ltr; text-align: left">.*.<\/span>/', ' ', $content);

but this code will replace all the content that beign with <span class="google-src-text" style="direction: ltr; text-align: left"> and last </span>.

Upvotes: 0

Views: 61

Answers (2)

Andresch Serj
Andresch Serj

Reputation: 37328

This one works if you have no HTML in your "any character".

/<span class="google-src-text" style="direction: ltr; text-align: left">([^<]{1,})<\/span>/

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

* is greedy by default, you need to change it to lazy, like so:

preg_replace('/<span class="google-src-text" style="direction: ltr; text-align: left">.*?<\/span>/', ' ', $content);
//                                                               Note the question mark ^

This will match *up to the first </span>, note that if you have a nested span inside, it will not fetch all the way to the end.

That's why You shouldn't parse HTML with Regex and should instead use a proper HTML DOM parser

Upvotes: 1

Related Questions