Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用正则表达式和ereg在数组中存储字符串的每个字? $str=“今天天气真好”; ereg(“\*一些正则表达式*\”,$str,$match); 对于($i=0;$i_Php_Regex - Fatal编程技术网

Php 如何使用正则表达式和ereg在数组中存储字符串的每个字? $str=“今天天气真好”; ereg(“\*一些正则表达式*\”,$str,$match); 对于($i=0;$i

Php 如何使用正则表达式和ereg在数组中存储字符串的每个字? $str=“今天天气真好”; ereg(“\*一些正则表达式*\”,$str,$match); 对于($i=0;$i,php,regex,Php,Regex,),您可以使用它来匹配单词: $str = "This is a beautiful day"; ereg("\*Some regular expression*\", $str, $match); for($i=0; $i<count($match); $i++) print("$match[$i] <br>"); 顺便说一句,您不应该使用ereg(),因此我将其替换为正确的函数调用。我建议您将var_dump($match[$I]);放入循环中,以便查看您正在使用的

),您可以使用它来匹配单词:

$str = "This is a beautiful day";
ereg("\*Some regular expression*\", $str, $match);
for($i=0; $i<count($match); $i++)
   print("$match[$i] <br>");
顺便说一句,您不应该使用
ereg()
,因此我将其替换为正确的函数调用。我建议您将
var_dump($match[$I]);
放入循环中,以便查看您正在使用的功能


注意:您特别要求使用正则表达式,而不是其他解决方案,因此我将把它留在那里。(但还有其他解决方案)(而且它们更好。)

为什么不呢?
preg_match_all(“~\w+~”,$str,$matches)
我希望它与POSIX标准一起使用。因此,ereg not preg
ereg()很早以前就被PHP否决了。请重新考虑一下。为什么不<代码>爆炸()/代码>?这个问题还不清楚(对我来说)。你认为一个词是什么?你的文本可以包含任何Unicode字母还是仅仅是ASCII字母?如果你只调用一个单词,任何非空白的文本块,<代码>爆炸(“”,$STR)。
是标准方法。如果您想使用正则表达式匹配所有Unicode点,可以使用
preg\u match\u all(“~\X+~u',$str,$matches)
。为什么我不应该使用ereg?这是一个不好的规则,还是不可能?PHP文档中有一个关于
ereg()
的红色警告:“从PHP 5.3.0开始,此功能已被弃用。强烈建议不要依赖此功能。”红色大警告通常表示您应该远离:)
preg_match_all("/(\w+)/", $str, $match);