Search code examples
phpregexyiiexplode

Explode based on special characters


I have an issue where, I am not able to explode based on database values.

my database values can be as such

  1. 1-10
  2. < 10
  3. ">20

I want to explode based on specail characters and put them in an array.

for example

$array = explode("/ (-) "/, Model::find()->findByPj($model->id)); 

How do I get the regex for that to explode dynamically based on the data


Solution

  • explode() can't handle regular expressions. You are looking for preg_split() and the correct pattern is (-|<|>). The pattern basically means: match - or < or >. So the code should look like this:

    $array = preg_split("/ (-|<|>) /", Model::find()->findByPj($model->id));