36

"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!
Jim
  • 22,354
  • 6
  • 52
  • 80
omg
  • 136,412
  • 142
  • 288
  • 348

4 Answers4

51
<?php

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

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

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
Dharman
  • 30,962
  • 25
  • 85
  • 135
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 1
    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 Sep 21 '09 at 03:37
  • Shouldn't the commas after the `'
    '` be periods, instead?
    – EvilChookie Sep 21 '09 at 03:38
  • 2
    @EvilChookie: I'm sending multiple arguments, I'm just used to doing it that way. – meder omuraliev Sep 21 '09 at 03:38
  • @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 Sep 21 '09 at 03:41
  • 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 omuraliev Sep 21 '09 at 03:43
  • Hey buddy,there is some tiny issue with your solution,see my update:) – omg Sep 21 '09 at 03:45
  • 1
    What character is that? That's not a semicolon. – meder omuraliev Sep 21 '09 at 03:48
  • Check out my updated version, I found the unicode equivalent for that character. – meder omuraliev Sep 21 '09 at 03:51
  • The code is the same as your update.Let me open another question anyway:) – omg Sep 21 '09 at 04:02
  • 1
    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 Sep 21 '09 at 06:20
16
$result_array = preg_split( "/[;,]/", $starting_string );
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Devin Ceartas
  • 4,743
  • 1
  • 20
  • 33
4

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

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

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

There are a few extra optional parameters which may be useful to you.

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'

Moritur
  • 1,651
  • 1
  • 18
  • 31
pavium
  • 14,808
  • 4
  • 33
  • 50
-1

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.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984