假设我有一个输入,例如:
$input = "This is some sample input, it's not complex. ";
$input .="But does contain punctuation such as full stops / back-slashes / etc";
$array = arrayFunction($input);
我的问题是:我需要做什么arrayFunction
才能$array
等于以下内容:
$array = array(
0 => "This",
1 => "is",
2 => "some",
3 => "sample",
4 => "input",
5 => ",",
6 => "it's",
7 => "not",
8 => "complex",
9 => ".",
10 => "But",
11 => "does",
12 => "contain",
13 => "punctuation",
14 => "such",
15 => "as",
16 => "full",
17 => "stops",
18 => "/",
19 => "back-slashes",
20 => "etc",
);
我一直在做以下事情
function arrayFunction($input)
{
$explode = explode( " ", $input );
$output = array();
foreach ( $explode as $word )
{
$output[] = trim( \String::lower( preg_replace('/[^\w|\s|-]+/', '', $word ) ) );
}
return $output;
}
这对我的需要很好,但现在我需要输出包含标点符号,以便我可以通过以下测试:
$input = "This is some sample input, it's not complex.";
$input .= "But does contain punctuation such as full stops/back-slashes/etc";
$array = arrayFunction($input);
$test = implode(' ', $array);
if ($test == $input) {
echo 'PASS';
} else {
echo 'FAIL';
}
谢谢。
编辑我正在考虑这样做的方式是按空格爆炸,然后循环该结果并通过标点符号进一步拆分。
编辑感谢下面接受的答案,我能够将我的代码重写为有效的东西。对于那些感兴趣的人,可以在这里看到https://gist.github.com/carbontwelve/6639350