我开发了一个带有laravel的系统。 这是我从csv文件中获取的字符串。我需要选择这个日期和时间。成阵列。如果我可以选择包含以下内容的单词:(14:13:27)我可以获得时间和方法相同的方法。
答案 0 :(得分:0)
一个简单的解决方案是 -
$string= "000001 0001 000000000000001975 00 02 0 000 2017/12/13 44:13:27";
preg_match("/([0-9]+):([0-5][0-9]):([0-5][0-9])/", $string, $matches);
echo $matches[0];
答案 1 :(得分:0)
尝试这种模式:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
// Echo session variables that were set on previous page
echo "Username is " . $_SESSION["username"] . ".<br>";
echo "Gender is " . $_SESSION["gender"] . ".";
?>
</body>
</html>
<html>
<body>
echo <a href="test4.php?$_SESSION['username']&$_SESSION['gender']" ); >Submit Username</a>
<!--This is test4.php-->
<?php
session_start();
$username = $_GET["username"]&$_GET["gender"];
//And then do whatever you want to do with it
?>
答案 2 :(得分:0)
我还不清楚OP的确切期望输出,但我对其他答案中的模式更加不知所措。我会发布这一系列解决方案以改善Stackoverflow,因为我找不到合适的副本来关闭。
我使用波浪线~
作为模式分隔符,因此模式中的/
字符不需要转义。另外,请注意我没有调用\K
来重新启动全字符串匹配,因为没有理由这样做。
代码:(Demo)
$string='000001 0001 000000000000001975 00 02 0 000 2017/12/13 14:13:27';
var_export(preg_match('~\d{4}/\d{2}/\d{2}~',$string,$out)?$out:[]); // date
echo "\n\n";
var_export(preg_match('~\d{2}:\d{2}:\d{2}~',$string,$out)?$out:[]); // time
echo "\n\n";
var_export(preg_match('~\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}~',$string,$out)?$out:[]); // full datetime
echo "\n\n";
var_export(preg_match('~(\d{4}/\d{2}/\d{2}) (\d{2}:\d{2}:\d{2})~',$string,$out)?$out:[]); // capture date and time
echo "\n\n";
var_export(preg_match_all('~\d{4}/\d{2}/\d{2}|\d{2}:\d{2}:\d{2}~',$string,$out)?$out:[]); // capture date or time
echo "\n\n";
var_export(preg_match('~(\d{4})/(\d{2})/(\d{2}) (\d{2}):(\d{2}):(\d{2})~',$string,$out)?$out:[]); // capture date digits and time digits
输出:
// date
array (
0 => '2017/12/13',
)
// time
array (
0 => '14:13:27',
)
full date time
array (
0 => '2017/12/13 14:13:27',
)
// capture date and time
array (
0 => '2017/12/13 14:13:27',
1 => '2017/12/13',
2 => '14:13:27',
)
// capture date or time
array (
0 =>
array (
0 => '2017/12/13',
1 => '14:13:27',
),
)
// capture date digits and time digits
array (
0 => '2017/12/13 14:13:27',
1 => '2017',
2 => '12',
3 => '13',
4 => '14',
5 => '13',
6 => '27',
)
P.S。对于未来的读者,如果您需要更强的日期验证,那么正则表达式可能不适合您的任务。