4Answers

Explode string php

7.3k Views
Answer

preg_split(), and its PREG_SPLIT_NO_EMPTY option, should do just the trick, here.

And great advantage : it'll skip empty parts even in the middle of the string -- and not just at the beginning or end of it.


The following portion of code :

$string = "|1|2|3|4|";
$parts = preg_split('/\|/', $string, -1, PREG_SPLIT_NO_EMPTY);
var_dump($parts);


Will give you this resulting array :

array
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => string '3' (length=1)
  3 => string '4' (length=1)
  • 4
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions