我想创建一个函数,从文件中的冒号后面获取每一行。我在切割所述字符处的字符串时遇到问题。
所以我想把这个切片:
“日期:2017 年 3 月 27 日”至“2017 年 3 月 27 日”“开始时间:中午 12:30”至“中午 12:30”...等等。
注意:我不需要编写实际函数的帮助,我只想知道如何在第一个冒号处拼接该行
对于您的情况,使用:$string = "date: march 27, 2017";或$string = "start: 12:30pm";
您可以选择以下任何一种技术:
*注意:如果担心针的存在(冒号或冒号空间),那么您应该使用防伪选项之一,否则需要额外的考虑才能在没有针的情况下捕捉绳子。
$string = ($pos = strpos($string, ": ")) ? substr($string, $pos + 2) : $string;
Run Code Online (Sandbox Code Playgroud)
$string = ($sub = strstr($string, ": ")) ? substr($sub, 2) : $string;
Run Code Online (Sandbox Code Playgroud)
使用explode() *需要存在冒号空间:
$string = explode(': ', $string, 2)[1];
Run Code Online (Sandbox Code Playgroud)
使用explode() & end() *不再是一句简单的话,而是防伪的:
$array = explode(': ', $string, 2);
$string = end($array);
// nesting explode() inside end() will yield the following notice:
// NOTICE Only variables should be passed by reference
Run Code Online (Sandbox Code Playgroud)
将preg_replace()与正则表达式模式一起使用*防伪:
$string = preg_replace("/^.*?:\s/", "", $string);
Run Code Online (Sandbox Code Playgroud)
将preg_match()与正则表达式模式一起使用,不是单行的,而是防伪的:
$string = preg_match("/^.*?:\s\K.*/", $string, $m) ? $m[0]: $string;
Run Code Online (Sandbox Code Playgroud)
这是一个演示,适合任何想要在自己的雪花案例上运行一些测试的人。
| 归档时间: |
|
| 查看次数: |
10406 次 |
| 最近记录: |