3

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

')));
3
  • 1
    Most frequently regular-expressions.info is recommended. The introductions are mostly understandable. Also look into this tools list stackoverflow.com/questions/89718/… for help with crafting regexpressions. Commented May 2, 2011 at 0:03
  • Creds for using delimiters. So many newcomers do not. :) Commented May 2, 2011 at 0:04
  • 1
    @mario I checked it before. It didnt look newcomer -friendly. I'll take a look again, Thanks :) Commented May 2, 2011 at 0:25

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The code does not replace anything or give any errors. Thanks for helping.
@Muazam: True. Try again with the /s modifier. Works on your example.
0

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);

3 Comments

Sorry for the misunderstanding, updated first post with an example.
@Muazam: I still don't really get it. Maybe I'm just too tired.
@Muazam: Your question didn't ask me to :(
0

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.

1 Comment

I want to match them both, but not in order. Thanks
0
  • Use parenthesis to create matching group
  • Use $1 to refer to it

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

1 Comment

syntax error, unexpected ']', That looks like some advance RegEX. Thanks for helping :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.