Christian Stewart
Christian Stewart

Reputation: 15519

PHP Split Problem

I am trying to use (and I've tried both) preg_split() and split() and neither of these have worked for me. Here are the attempts and outputs.

preg_split("^", "ItemOne^ItemTwo^Item.Three^");
//output - null or false when attempting to implode() it.
preg_split("\^", "ItemOne^ItemTwo^Item.Three^");
//output - null or false when attempting to implode() it. Attempted to escape the needle.
//SAME THING WITH split().

Thanks for your help... Christian Stewart

Upvotes: 0

Views: 676

Answers (4)

Niki
Niki

Reputation: 1924

Since you are using preg_split you are trying to split the string by a given regular expresion. The circumflex (^) is a regular expression metacharacter and therefore not working in your example.

btw: preg_split is an alternative to split and not deprecated.

Upvotes: 0

Josh Leitzel
Josh Leitzel

Reputation: 15199

Are you sure you're not just looking for explode?

explode('^', 'ItemOne^ItemTwo^Item.Three^');

Upvotes: 1

LesterDove
LesterDove

Reputation: 3044

Try

explode("^", "ItemOne^ItemTwo^Item.Three^");

since your search pattern isn't a regex.

Upvotes: 1

Elle H
Elle H

Reputation: 12217

split is deprecated. You should use explode

$arr = explode('^', "ItemOne^ItemTwo^Item.Three^");

Upvotes: 1

Related Questions