Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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在整数和字母之间分解_Php_Regex_Explode - Fatal编程技术网

PHP在整数和字母之间分解

PHP在整数和字母之间分解,php,regex,explode,Php,Regex,Explode,有没有可能,比如说,遍历这个列表,然后在找到的第一个字母和第一个整数之间分解,这样我就可以将文本从数字集中分离出来,最后得到如下结果: array ( [0] => 3 / 4 Bananas [1] => 1 / 7 Apples [2] => 3 / 3 Kiwis ) 我不知道您将如何将其指定为分隔符。有可能吗 array ( [0] => Bananas [1] => Apples [2] =>

有没有可能,比如说,遍历这个列表,然后在找到的第一个字母和第一个整数之间分解,这样我就可以将文本从数字集中分离出来,最后得到如下结果:

array (    
    [0] => 3 / 4 Bananas
    [1] => 1 / 7 Apples
    [2] => 3 / 3 Kiwis
    )
我不知道您将如何将其指定为分隔符。有可能吗

array (
   [0] => Bananas
   [1] => Apples
   [2] => Kiwis
   )
编辑:更新的示例。用一个空间爆炸是行不通的。请参见上面的示例。

类似的内容:

foreach ($fruit_array as $line) {
   $var = explode("??", $line);
}
// An array to hold the result
$result = array();

// Loop the input array
foreach ($array as $str) {

  // Split the string to a maximum of 2 parts
  // See below for regex description
  $parts = preg_split('/\d\s*(?=[a-z])/i', $str, 2);

  // Push the last part of the split string onto the result array
  $result[] = array_pop($parts);

}

// Display the result
print_r($result);

您可以使用
preg\u match
而不是
explode

foreach ($fruit_array as $line) {
   $var = explode(" ", $line);
   $arr[$var[2]] = $var[3];
}

var_dump( $arr ) should output:

array (
   [0] => Bananas
   [1] => Apples
   [2] => Kiwis
   )
它将几乎完全匹配您的表达式,即一个数字
\d
,后跟一个或多个非字母
[^a-Za-z]
,后跟一个或多个字母或空格(用于解释多个单词)
[a-Za-z\s]+
。括号之间的最终匹配字符串将在第一次匹配中捕获,即,
$match[1]

这里有一个

$fruit_array = array("3 / 4 Bananas", "1 / 7 Apples", "3 / 3 Kiwis");
$result = array();
foreach ($fruit_array as $line) {
   preg_match("/\d[^A-Za-z]+([A-Za-z\s]+)/", $line, $match);
   array_push($result, $match[1]);
}
正则表达式的工作原理如下:

foreach ($fruit_array as $line) {
   $var = explode("??", $line);
}
// An array to hold the result
$result = array();

// Loop the input array
foreach ($array as $str) {

  // Split the string to a maximum of 2 parts
  // See below for regex description
  $parts = preg_split('/\d\s*(?=[a-z])/i', $str, 2);

  // Push the last part of the split string onto the result array
  $result[] = array_pop($parts);

}

// Display the result
print_r($result);

如果要“在找到的第一个字母和第一个整数之间”分解,则不应使用“分解”

接受分隔符作为其第一个参数:

数组分解(字符串$delimiter,字符串$string[,int$limit])

这意味着它不够“聪明”,无法理解像“在找到的第一个字母和第一个整数之间”这样的复杂规则,它只理解“在'1'上拆分”或“在'a'上拆分”之类的内容。分隔符必须是具体的:例如,一个特定的字母和一个特定的整数。(即“在字母'B'和整数'4'之间”)

对于更抽象/一般的内容,如您所描述的(“在找到的第一个字母和第一个整数之间”),您将需要一个模式。因此,最好的选择是使用或替代:


您也可以在PREG\u match中使用PREG\u OFFSET\u CAPTURE标志:

$a = array('1/4 banana', '3/5 apple', '3/2 peach');

foreach ($a as $b) {
    preg_match('/[a-z]/', $b, $matches, PREG_OFFSET_CAPTURE);
    $pos = $matches[0][1]; // position of first match of [a-z] in each case
    $c[] = substr($b, $pos);  
}

print_r($c);


Array ( [0] => banana [1] => apple [2] => peach )

列表的格式是什么,是字符串还是数组?如果水果名有两个单词,如
青苹果
,该怎么办?您的示例将只匹配
Green
@Norse:只需在
[A-Za-z]
中添加一个额外的
\s
来匹配空白。这样,它还可以匹配多个单词。我已经编辑了我的答案和演示。谢谢你的解释