我有句话说
":林志刚\ S717613 IB CUST.“
我要摘取林志刚的名字。这是在使用php preg匹配或任何其他规则的任何数字或特殊字符之前或之后的第一个单词。
句子可以是动态的。
发布于 2017-10-06 11:02:52
$str = ": Lim Chee Kong \ S717613 IB CUST.";
$res = preg_split("/[^A-Za-z]/",$str);
$result = Array();
$wordFound = false;
$noMoreWords = false;
foreach($res as $key=>$words) {
if($words != '' && !$noMoreWords) {
$result[] = $words;
$wordFound = true;
}
if($wordFound && $words == '') {
$noMoreWords = true;
}
}
echo implode(" ",$result); //Lim Chee Kong发布于 2017-10-06 11:04:20
正则表达式的定义与示例不兼容。“那是第一个词.”-> Lim Chee Kong不是一个词。它是一组单词,由空白"...before“或任意数量的特殊字符”->“分隔--这对于示例中的子字符串"IB CUST”也是如此。
尽管如此,以下是匹配一组可选地被特殊字符包围的单词:
\W*\b([a-zA-Z\s]+)\b\W*在您的示例中,这将得到"Lim Chee Kong“和"IB CUST”。
发布于 2017-10-06 11:20:45
正如您注意到的,名称是从数组中回显出来的,您可以这样做:(我不明白为什么要分割它)。
以您的数组为例:
$array = ['name' => 'Lim Chee Kong', 'extra' => 'S717613 IB CUST'];Echo it:
echo ' ":' . $array['name'] . ' \ ' . $array['extra'] . '." ';结果:
":林志刚\ S717613 IB CUST.“
数组可以包含要回显的其他信息。这就是你需要的吗?
https://stackoverflow.com/questions/46604029
复制相似问题