これは、検索者の参照のための私の完全なソリューションです。
<?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 はこちら
)