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.

web.curious.gk
  • 147
  • 3
  • 4
  • 15

2 Answers2

6

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
  • 161,348
  • 33
  • 346
  • 320
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,276
  • 2
  • 15
  • 37