我希望使用 PHPmb_split
来拆分以冒号分隔的字符串,但忽略转义的冒号(即\:
)。
例如:
Part A is here:Part B\: is over here:Part C\ is here:Part \\:D is here
需要拆分成:
Part A is here
Part B: is over here
Part C\ is here
Part \:D is here
我真的不知道该怎么做。有什么建议吗?
You can try splitting on:
(?<!\\):
(?<!\\)
is a negative lookbehind and will make :
not match if it is preceeded by \
.
[Note I never tried mb_split
, but this should work with preg_split
]
这是我供搜索者参考的完整解决方案。
<?php
if (!function_exists('mb_str_replace')) {
function mb_str_replace ($needle, $replacement, $haystack) {
// Courtesy of marc at ermshaus dot org: http://www.php.net/manual/en/ref.mbstring.php#86120
$needleLength = mb_strlen($needle);
$replacementLength = mb_strlen($replacement);
$pos = mb_strpos($haystack, $needle);
while ($pos !== false) {
$haystack = mb_substr($haystack, 0, $pos).$replacement.mb_substr($haystack, $pos+$needleLength);
$pos = mb_strpos($haystack, $needle, $pos+$replacementLength);
}
return $haystack;
}
}
$str = <<<EOD
Part A is here:Part B\: is over here:Part C\ is here:Part \\\:D is here
EOD;
/* Why did I have to use "\\\:D" in a heredoc? No idea.
I thought "\\:D" would be sufficient, but for some
reason the backslash was parsed anyways. Which kinda
makes heredocs useless to me. */
echo "$str<br>", PHP_EOL;
$arr = mb_split('(?<!\\\):', $str);
// Courtesy of Jerry: http://stackoverflow.com/users/1578604/jerry
foreach ($arr as &$val) {
$val = mb_str_replace('\:', ':', $val);
}
unset($val);
echo '<pre>', print_r($arr, true), '</pre>';
?>
输出:
A部分在这里:B部分\:在这里结束:C部分在这里:部分\\:D在这里 大批 ( [0] => A 部分在这里 [1] => B 部分:在这里 [2] => C 部分在这里 [3] => 部分 \:D 在这里 )