简体   繁体   English

preg_replace PHP 和

[英]preg_replace PHP AND

Is there a AND operator for PHP - regular expression. PHP 是否有AND运算符 - 正则表达式。

I'm trying to replace everything from document to ' AND ) .我正在尝试替换从document' AND )的所有内容。

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

Any idea how this can be done?知道如何做到这一点吗?

I've tried to find some tutorial for RegEX but I can't find any good.我试图为 RegEX 找到一些教程,但我找不到任何好的。 If you have any good sites or book please give me a link.如果你有什么好的网站或书籍,请给我一个链接。 I googled a lot.我用谷歌搜索了很多。

Thanks.谢谢。

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

')));

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.因此,它匹配包含在('')中的任何内容以及不同数量的后者。

Do you mean OR ?你是说OR吗?

You want to strip ranges ["document", "'"] and ranges ["document", ")"] , right?您想删除范围["document", "'"]和范围["document", ")"] ,对吗? Which is like the range ["document", "'" OR ")"] .这就像范围["document", "'" OR ")"]

In regular expressions, |在正则表达式中, | means "or".意思是“或”。

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

In regular expression, AB is A and B A|B is A OR B在正则表达式中,AB 是A and B A|B 是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.使用http://gskinner.com/RegExr/ 之类的东西来尝试你的正则表达式。 It also has descriptions on the right.右侧也有说明。

  • Use parenthesis to create matching group使用括号创建匹配组
  • Use $1 to refer to it使用 $1 来引用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM