views:

1326

answers:

4

"something here ; and there, oh,that's all!"

I want to split it by ; and ,

so after processing should get:

something here

and there

oh

that's all!

+2  A: 

$result_array = preg_split( "/[;,]/", $starting_string );

Devin Ceartas
+7  A: 
<?php

$pattern = '/[;,]/';

$string = "something here ; and there, oh,that's all!";

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';

Updated answer to an updated question:

<?php

$pattern = '/[\x{ff0c},]/u';

//$string = "something here ; and there, oh,that's all!";
$string = 'hei,nihao,a ';


echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
meder
If you want print_r() to return a string to be echo'd, the *proper* way to do it is to pass in a 2nd argument that evaluates to true. e.g. `print_r($array, true);`
alex
Shouldn't the commas after the `'<pre>'` be periods, instead?
EvilChookie
missed that, thanks.
meder
@EvilChookie: I'm sending multiple arguments, I'm just used to doing it that way.
meder
@EvilChookie - *they* even say it's faster to send multiple arguments then to concatenate. However, I'd *think* the difference would be minimal. Feel free to prove me wrong!
alex
Call me nuts but personally it's just faster to use my middle finger and hit the comma than my ring finger on the period, the speed difference between multiple arguments and concatenation is probably so minimal that you'll never notice unless you're doing multiple nested loops or something of that nature, it's possible there won't even be a noticeable difference then.
meder
Hey buddy,there is some tiny issue with your solution,see my update:)
Shore
What character is that? That's not a semicolon.
meder
Check out my updated version, I found the unicode equivalent for that character.
meder
The result is not right:Array( [0] => hei,nihao,a)
Shore
Paste your updated code.
meder
The code is the same as your update.Let me open another question anyway:)
Shore
I just wasn't sure if it was a typo, or if that were actually a proper way to perform an echo (which it clearly is). Thanks for clarifying :P
EvilChookie
A: 

You can get the values into an array using Devin's or Meder's method.

To get the output you want, you could probably do this

echo implode("\n", $resultingArray);

Or use <br /> if it's HTML you want.

alex
A: 

The split() PHP funcition allows the delimiter to be a regular expression. Unfortunately it's deprecated and will be removed in PHP6

The preg_split() function should be OK, and it returns an array:

$results = preg_split('[;,]/', $string);

and there are a few extra optional parameters which may be useful.

Is the first delimiter character in your edited example actually a 2 byte Unicode character?

Perhaps the preg_slit() function is treating the delimiter as three characters and splitting between the characters of the unicode (Chinese?) 'character'

pavium