我有以下字符串:
*/3 * * * * echo yes我想要做的是创建一个包含2个元素的数组,其中元素在第5个字段之后彼此分开:
$result = [ "*/3 * * * *", "echo yes" ];在Linux bash shell中我会使用:
echo "*/3 * * * * logger yes" | cut -d' ' -f1-5 echo "*/3 * * * * logger yes" | cut -d' ' -f6-你可以看到它是一个cron-entry。 因此,编写RegEx会非常不方便,因为字段可能会发生变化。 好吧,我可以编写一个RegEx,它用空白分隔符将元素彼此分开,但我想知道PHP中是否有任何类似于GNU cut或awk函数,我可以使用它来分割这两个部分在第5场之后。
我怎么能用PHP实现相同的结果?
I have the following string:
*/3 * * * * echo yesWhat I want to do is to create an array with 2 elements from that, where the elements are separated after the 5th field from each other:
$result = [ "*/3 * * * *", "echo yes" ];In Linux bash shell I would have just used:
echo "*/3 * * * * logger yes" | cut -d' ' -f1-5 echo "*/3 * * * * logger yes" | cut -d' ' -f6-As you can see it's an cron-entry. So writing a RegEx would be rather inconvenient as the fields can change. Ok, I could write a RegEx which splits the elements from each other with a whitespace delimiter, but I'm wondering is there any function in PHP which is similar too GNU cut or awk, which I could use for that which splits the two parts after the 5th field.
How could I achieve the same result with PHP?
最满意答案
尝试这个 :
$result = [ implode('', array_slice(explode(' ', $string), 0, 5), implode('', array_slice(explode(' ', $string), 5) ];Try this :
$result = [ implode('', array_slice(explode(' ', $string), 0, 5), implode('', array_slice(explode(' ', $string), 5) ];更多推荐
发布评论