English 中文(简体)
如何在无意外匹配的情况下在PHP中找到一个全字?
原标题:How to find a whole word in a string in PHP without accidental matches?

Parsing an array of string commands, I need to know if a string contains a specific keyword.

我知道很简单,但问题是当指挥关键词也可能是另一个词的一部分时。

Ex:

CHECKSOUND
SOUND
CHECK

因此,我需要检查目前的线路是否由CHECKSOUND、SOUND或CHECK指挥。

如果我这样说:

if(stristr($line, SOUND ) == true)

然后可在<条码>上查询,从而不能正确分类。

Question:

是否只有像SOUND这样一整字才出现,如果把发生SOUND视为像CHECKSOUND这样另一词的一部分,则会忽略这种情况?

我相信,我在这里没有一件简单的事情。

最佳回答

你们可以定期表达,以轻易达到目标。

Example #2 from the documentation of preg_match:

/* The  in the pattern indicates a word boundary, so only the distinct
 * word "web" is matched, and not a word partial like "webbing" or "cobweb" */
 if (preg_match("/web/i", "PHP is the web scripting language of choice.")) {
     echo "A match was found.";
 } else {
     echo "A match was not found.";
 }

请注意,上述例子使用<代码>i modifier,这使查询“web”案件不敏感。

问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签