Forum Moderators: coopster

Message Too Old, No Replies

Splitting a string before and after a character

regex string php split utf-8

         

tomroberts

2:03 pm on Jun 23, 2010 (gmt 0)

10+ Year Member



Hi everyone,

I'm trying to split a unicode string before and after a particular character (or characters).

Basically, given a string like this:

trademark® some more text


I'd like to end up with an array like this, preserving whitespace:


Array
(
[0] => trademark
[1] => ®
[2] => some more text
)


I was thinking I might be able to use preg_split, but I can't figure out how to write the regex (I can split after the character but not before) and I'm not sure if I can use a unicode character in an expression.

Apologies if this is blindly obvious, I've not had to use regular expressions much before.

Thanks in advance,
Tom

Readie

4:26 pm on Jun 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, both preg_split and explode remove what they split by. A custom function would work though:

[pre]function my_explode($delimiter, $input) {
$pieces = explode($delimiter, $input);
$count = count($pieces);
$output = array();
for($i = 0; $i < $count; $i++) {
if($i !== 0) { $output[] = $delimiter; }
$output[] = $pieces[$i];
}
return $output;
}[/pre]

tomroberts

10:41 am on Jun 24, 2010 (gmt 0)

10+ Year Member



That's great (and irritatingly simple), thanks Readie!