开发者

PHP regex, space or no character

开发者 https://www.devze.com 2023-01-08 18:00 出处:网络
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:

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:

p开发者_运维知识库reg_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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消