PHP: split()

 

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>php split Test</title>
</head>
<body>


<?php
// from https://www.php.net/manual/zh/function.preg-split.php
//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
$pattern = '/[\s,:#]+/';
$geovindu="hypertext language, programming geovindu#涂聚文";
$keywords = preg_split("/[\s,:,#]+/", "hypertext language, programming geovindu#涂聚文");
$du="<br/>";
print_r($keywords);
echo ($keywords[0].$du);
echo ($keywords[1].$du);
echo ($keywords[2].$du);
echo ($keywords[3].$du);
echo ($keywords[4].$du);
?>

<?php
$search_expression = "apple bear \"Tom Cruise\" or 'Mickey Mouse' another word,涂聚文,GeovinDu";
$words = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($words);
$geo=' ';
if(empty(trim($geo)))
{
    echo ("为空值");
}
?>

</body>
</html>

  

posted @ 2023-05-06 13:01  ®Geovin Du Dream Park™  阅读(15)  评论(0编辑  收藏  举报