1

使用 PHP,如果您有一个stringwhich 点后可能有也可能没有空格,例如:

"1. one 2.too 3. free 4. for 5.five "

您可以使用什么函数来创建array如下:

array(1 => "one", 2 => "too", 3 => "free", 4 => "for", 5 => "five") 

键是列表项编号(例如array上面没有0

我认为需要一个正则表达式,也许使用preg_split或类似的?我不擅长正则表达式,所以任何帮助将不胜感激。

4

2 回答 2

2

关于什么:

$str = "1. one 2.too 3. free 4. for 5.five ";
$arr = preg_split('/\d+\./', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);
于 2012-09-29T17:53:35.433 回答
-1

我得到了一个快速破解,它似乎对我来说工作正常

        $string = "1. one 2.too 3. free 4. for 5.five ";

    $text_only = preg_replace("/[^A-Z,a-z]/",".",$string);
    $num_only = preg_replace("/[^0-9]/",".",$string);

    $explode_nums = explode('.',$num_only);
    $explode_text = explode('.',$text_only);

    foreach($explode_text as $key => $value)
    {
        if($value !== '' && $value !== ' ')
        {
            $text_array[] = $value;
        }
    }

    foreach($explode_nums as $key => $value)
    {
        if($value !== '' && $value !== ' ')
        {
            $num_array[] = $value;
        }
    }

    foreach($num_array as $key => $value)
    {
        $new_array[$value] = $text_array[$key];
    }

    print_r($new_array);

测试一下,让我知道是否工作正常

于 2012-09-29T18:15:17.483 回答