I have an issue where, I am not able to explode based on database values.
my database values can be as such
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
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));
You can use preg_split(regex_pattern, string)
like this:
$array = preg_split("(-)", 'some string here, lorem-ipsum');
var_dump($array);
Hope this helps!