PHP 正则表达式、空格或无字符
好吧,我试图找出这个正则表达式,其中有一个单词,并且在单词的两端可以是空格或没有字符。这是一个例子:
preg_match_all("/( ?)(" . $piece . ")( ?)/is", $fk, $sub);
其中 ( ?)
是我希望它是“只能是空格或根本没有字符的单个字符”。我基本上试图创建一个函数,根据其周围的字符来检查某物是否是单词。 $piece
是这个词,所以它必须是单独的,而不是另一个更长的词的一部分,如果你明白我的意思的话。谢谢
Ok Im trying to figure out this regex where I have a word, and on either end of the word it can be a space or no character. Heres an example:
preg_match_all("/( ?)(" . $piece . ")( ?)/is", $fk, $sub);
Where ( ?)
is I want that to be "A single character that can only be a space or no character at all". Im trying to basically make a function that checks whether something is a word or not based on its surrounding characters. And $piece
is the word, so It has to be by itself, not part of another longer word if you know what I mean. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要测试空格或无字符,请使用以下语法:
preg_match_all("/(^| )(" . $piece . ")( |$)/is", $fk, $sub);
(^| )
表示:匹配字符串的开头(所谓的“无字符”)或空格。( |$)
的意思是:匹配空格或字符串结尾(同样,“无字符”)。字符串的开头和结尾是唯一没有字符的地方。To test for a space or no character, use the following syntax:
preg_match_all("/(^| )(" . $piece . ")( |$)/is", $fk, $sub);
The
(^| )
means: Match either beginning of string (so called "no character") or space. The( |$)
means: Match space or end of string (again, a "no character"). The beginning and end of a string are the only places where there is no character.