php transform string 1|*|3|*|150 to another format [“1”,“3”,“150”]

171
January 13, 2018, at 07:58 AM

I have a php code to read multiselect values from database. the value maybe a single string like 1 or 2 or ... etc . or maybe a multi as 1|*|3|*|150 . I want a php code to check value if single , so it remain single. but if multi as 1|*|3|*|150 so it convert to ["1","3","150"] How can I do that / thank you

Answer 1

It's just explode. explode can use a delimiter consisting of multiple characters.

Your string looks fancier than just comma-separated values, but the delimiter appears to be consistent, so

$array = explode('|*|', $string);

should work just fine.

It seems I misunderstood the question initially, and you don't want an array as the result, you want a sort of string representation of an array. In that case, you can just str_replace the delimiter instead of using explode.

$string = '["' . str_replace('|*|', '","', $string) . '"]';

Some unsolicited advice, for you or anyone else who reads this later: In general this is not how I would recommend storing multiple associated values in a database. See this great answer for some compelling reasons not to do this:

Is storing a delimited list in a database column really that bad? (Spoiler alert: Yes.)

Rent Charter Buses Company
READ ALSO
Laravel 5.4 Queue Email

Laravel 5.4 Queue Email

I'm trying to use Laravel Queues for sending emails using the database driver, I have already configured it, run the migration for the "jobs" table and when I run this:

241
Symfony 4: doctrine in command

Symfony 4: doctrine in command

I am using symfony 4 and I want to access a repository for an entity if I am in the Command classThere is not a function getDoctrine or something

448
why the while loop runs once?

why the while loop runs once?

the following code is only running once , while the number of times it should run is 4 , any help ?

232
Trying to get property of non-object Notice in custom avatar function for Wordpress

Trying to get property of non-object Notice in custom avatar function for Wordpress

The following custom avatar functions works fine, but I get the following notice

241