1

I have a record with a value of

Name::Address::Job Description

how do I remove the :: and put all the data into an array like

array("Name","Address","Job Description")

Thank a lot.

webdev.gk
  • 157
  • 3
  • 4
  • 15
  • possible duplicate of [How can I split a comma delimited string into an array in PHP?](http://stackoverflow.com/questions/1125730/how-can-i-split-a-comma-delimited-string-into-an-array-in-php) – zaf Nov 17 '11 at 09:20

2 Answers2

5

You can use explode, like this:

$str = 'Name::Address::Job Description';
$delimiter = '::';
$array = explode($delimiter, $str);

To perform the replacement, you can use str_replace:

$str = str_replace($delimiter, '', $str); 

Alternatively, you can simply implode on the return value of explode:

$str = implode($array);
Jacob Relkin
  • 156,685
  • 31
  • 339
  • 316
1

For more advanced splitting you can use

$str = 'Name::Address::Job Description';
$delimiter = '::';
$array = preg_split("/$delimiter/",$str);

Array
(
    [0] => Name
    [1] => Address
    [2] => Job Description
)

preg_split is a Regex split while explode is a string split.

Regex would be useful if you had variation in your data: IE

$str = 'Name::Address:Job Description;Job Title';
$delimiter = '::?|;';
$array = preg_split("/$delimiter/",$str);


Array
(
    [0] => Name
    [1] => Address
    [2] => Job Description
    [3] => Job Title
)
c3cris
  • 1,194
  • 2
  • 14
  • 36