type\:text:content\:some_data
$in[]='type:text';
$in[]='content:some_data';
for($i=0; isset($in[$i]); $i++){
$in[$i]=str_replace(array('\', ':'), array('\\', '\:'), $in[$i]);
}
$string=implode(':', $in); //$string=='type\:text:content\:some_data'
1. type\
2. text
3. content\
4. some_data
а надо1. type:text
2. content:some_data
function custom_split($str) {
$parts = preg_split('/(?<![^\\\\]\\\\):/', $str);
array_walk($parts, function(&$v) { $v = str_replace('\\:', ':', $v); });
return $parts;
}
var_dump(custom_split('a:b:c:d\\:h:g'));
var_dump(custom_split('a:b:c:d\\\\:h:g'));
> array (size=5)
> 0 => string 'a' (length=1)
> 1 => string 'b' (length=1)
> 2 => string 'c' (length=1)
> 3 => string 'd:h' (length=3)
> 4 => string 'g' (length=1)
> array (size=6)
> 0 => string 'a' (length=1)
> 1 => string 'b' (length=1)
> 2 => string 'c' (length=1)
> 3 => string 'd\\' (length=3)
> 4 => string 'h' (length=1)
> 5 => string 'g' (length=1)