Muazam
Muazam

Reputation: 379

Replace javascript document.write() call from a HTML document body with its HTML content

Is there an AND operator for PHP's regular expressions?

I'm trying to replace everything from document to ' AND ).

$output = preg_replace('/document.*\'/', '', $output);

I've tried to find some tutorial for regex, but I can't find anything good.

EDIT: Misunderstanding.

This is the code before replaced.

<p>document.write(unescape('
<embed src="XXXXXX" type="application/x-shockwave-flash" wmode="window" width="712" height="475"%.35" allowFullScreen="true" ></embed>
')));</p>

I want to make it look like this:

<p>
<embed src="XXXXXX" type="application/x-shockwave-flash" wmode="window" width="712" height="475"%.35" allowFullScreen="true" ></embed>
</p>

Replaced:

document.write(unescape('

and

')));

Upvotes: 3

Views: 291

Answers (4)

mario
mario

Reputation: 145482

What you actually want is to replace two parts, and leave something in between over. To not make it match undesired parts, use explicit character classes:

= preg_replace("/document[\w.(]+['](.*?)['][);]+/s", '$1', $output); 

So it matches anything enclosed in (' and ') with varying amounts of the latter.

Upvotes: 2

vartec
vartec

Reputation: 134581

  • Use parenthesis to create matching group
  • Use $1 to refer to it

$output = preg_replace("/document[^']*'([^']*)[^;]*;/", '$1', $output);

Upvotes: 0

manojlds
manojlds

Reputation: 301127

In regular expression, AB is A and B A|B is A OR B

So if you want to match ') then just put that in the regular expression.

On the other hand if you want to match either ' or ) as the end of your string use '|) or [')]

Use something like http://gskinner.com/RegExr/ to try out your regexes. It also has descriptions on the right.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Do you mean OR?

You want to strip ranges ["document", "'"] and ranges ["document", ")"], right? Which is like the range ["document", "'" OR ")"].

In regular expressions, | means "or".

$output = preg_replace('/document.*(\'|\))/', '', $output);

Upvotes: 0

Related Questions