php 可选的空白正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14293024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optional Whitespace Regex
提问by jameslfc19
I'm having a problem trying to ignore whitespace in-between certain characters. I've been Googling around for a few days and can't seem to find the right solution.
我在尝试忽略某些字符之间的空格时遇到问题。我已经搜索了几天,似乎找不到正确的解决方案。
Here's my code:
这是我的代码:
// Get Image data
preg_match('#<a href="(.*?)" title="(.*?)"><img alt="(.*?)" src="(.*?)"[\s*]width="150"[\s*]height="(.*?)"></a>#', $data, $imagematch);
$image = $imagematch[4];
Basically these are some of the scenarios I have:
基本上这些是我遇到的一些场景:
<a href="/wiki/File:Sky1.png" title="File:Sky1.png"><img alt="Sky1.png" src="http://media-mcw.cursecdn.com/thumb/5/56/Sky1.png/150px-Sky1.png"width="150" height="84"></a>
(Notice the lack of a space between width="" and src="")
(注意 width="" 和 src="" 之间缺少空格)
And
和
<a href="/wiki/File:TallGrass.gif" title="File:TallGrass.gif"><img alt="TallGrass.gif" src="http://media-mcw.cursecdn.com/3/34/TallGrass.gif" width="150"height="150"></a>
(Notice the lack of a space in between width="" and height="".)
(注意 width="" 和 height="" 之间缺少空格。)
Is there anyway to ignore the whitespace in between those characters? As I am not a Regex expert.
无论如何要忽略这些字符之间的空格?因为我不是正则表达式专家。
回答by Naveed S
Add a \s?if a space can be allowed.
添加一个\s?如果可以允许空格。
\sstands for white space
\s代表空白
?says the preceding character may occur once or not occur.
? 表示前面的字符可能出现一次或不出现。
If more than one spaces are allowed and is optional, use \s*.
如果允许多个空格并且是可选的,请使用\s*.
*says preceding character can occur zero or more times.
*表示前面的字符可以出现零次或多次。
'#<a href\s?="(.*?)" title\s?="(.*?)"><img alt\s?="(.*?)" src\s?="(.*?)"[\s*]width\s?="150"[\s*]height\s?="(.*?)"></a>#'
allows an optional space between attribute name and =.
允许在属性名称和 = 之间有一个可选的空格。
If you want an optional space after the =also, add a \s?after it also.
如果你想要在=also之后有一个可选的空格,也可以\s?在它之后添加一个。
Likewise, wherever you have optional characters, you can use ?if the maximum occurrence is 1or *if the maximum occurrence is unlimited, following the optional character.
同样,只要有可选字符,就可以使用?if 最大出现次数为1或*最大出现次数不受限制,紧跟可选字符。
And your actual problem was [\s*]which causes occurrence of a whitespaceor a *as characters enclosed in [and ]is a character class. A character class allows occurrence of any of its members once (so remove *from it) and if you append a quantifier (?, +, *etc) after the ]any character(s) in the character class can occur according to the quantifier.
而您的实际问题是[\s*]导致出现空格或*作为包含在[其中]的字符并且是字符类。字符类允许其任何成员的一次出现(因此除去*来自它),如果你追加一个量词(?,+,*后等)]在字符类可以根据量词发生的任何字符(多个)。

