preg_replace 突然停止区分

发布于 2024-11-01 13:17:49 字数 433 浏览 4 评论 0原文

困惑了。我一直在使用下面的 IF PREG_MATCH 来区分整个单词和其他单词的一部分。它突然停止在该脚本以及我使用的依赖于该命令的任何其他脚本中运行。

结果是它找到了部分单词,尽管您可以看到它被明确告知只能查找整个单词。

$word = preg_replace("/[^a-zA-Z 0-9]+/", " ", $word);                                        

if (preg_match('#\b'.$word.'\b#',$goodfile) && (trim($word) != ""))  { 

        $fate = strpos($goodfile,$word);
        print $word ."  ";
        print $fate ."</br>";

Confounded. I've been using the below IF PREG_MATCH to distinguish between words which entire words and words which are parts of other words. It has suddenly ceased to function in this script, and any other script I use, which depend on this command.

The result is it finds parts of words, although you can see it is explicitly told to find only entire words.

$word = preg_replace("/[^a-zA-Z 0-9]+/", " ", $word);                                        

if (preg_match('#\b'.$word.'\b#',$goodfile) && (trim($word) != ""))  { 

        $fate = strpos($goodfile,$word);
        print $word ."  ";
        print $fate ."</br>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苹果你个爱泡泡 2024-11-08 13:17:49

如果您只想读取文本文件一行的第一个单词,就像您的标题所示,请尝试另一种方法:

// Get the file as an array, each element being a line
$lines = file("/path/to/file"); 

// Break up the first line by spaces
$words = explode(" ", $lines[0]); 

// Get the first word
$firstWord = $words[0]; 

If you only want to read the first word of a line of a text file, like your title suggests, try another method:

// Get the file as an array, each element being a line
$lines = file("/path/to/file"); 

// Break up the first line by spaces
$words = explode(" ", $lines[0]); 

// Get the first word
$firstWord = $words[0]; 
一场信仰旅途 2024-11-08 13:17:49

这比爆炸更快更干净,而且你不会制作任何数组

$first_word = stristr($lines, ' ', true); 

This would be faster and cleaner than explode and you won't be making any array

$first_word = stristr($lines, ' ', true); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文