我有字符串数组:
array('name is required','email is required','email is not valid','ip is not valid')
将这些字符串连接成单个字符串的最佳方法是什么:
name, email is required. email, ip is not valid
我有字符串数组:
array('name is required','email is required','email is not valid','ip is not valid')
将这些字符串连接成单个字符串的最佳方法是什么:
name, email is required. email, ip is not valid
The function you are looking for is called implode().
$str = implode(',',$array);
Although I didn't get the question at first, you better stick to this one. Lexical parser is not what you need for simple error messages output.
在这里我给个建议
<?php
define("DELIMITER", "is");
$a = array('name is required','email is required','email is not valid','ip is not valid');
foreach($a as $k=>$v){
$arr = explode(DELIMITER, $v, 2);
$subject = trim($arr[0]);
$description = trim($arr[1]);
$res[$description][] = $subject;
}
foreach($res as $k=>$v){
echo implode(",", $v)." ".DELIMITER." ".$k.". ";
}
echo "\n";