1

I have this string

$names = "The names are: [Yossi] & [Jane] & [Mono] & [Kiko]"

and I want my input to be:

array / list => [Yossi, Jane, Mono, Kiko]

I tried using some function from here: link

But all I got was Yossi.

I understand the reasone that all I see from those functions is "Yossi" but I couldn't figure out I can I get the rest of the names in some sort of an array or some kind of a list.

Thanks!

Aviv
  • 193
  • 2
  • 11

2 Answers2

3

Use a regular expression /\[[a-zA-Z]*\]/

preg_match_all('/\[[a-zA-Z]*\]/', $names, $matches);

your substrings will be saved in $matches[0]

See PHP Docs for preg_match_all

random_user_name
  • 25,694
  • 7
  • 76
  • 115
MrByte11
  • 300
  • 2
  • 9
  • 2
    A super-useful tool that I love is regex101.com - [Here is this Regular Expression in regex101](https://regex101.com/r/IvfCtr/1) so you can see exactly how that regex is processed.... – random_user_name May 07 '18 at 21:07
0
$names="The names are: [Yossi] & [Jane] & [Mono] & [Kiko]";
$clean1=explode(': ', $names);
$clean2=str_replace("] & ["," ",$clean1[1]);
$result=explode(" ", $clean2);
Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
  • 1
    Kudos for resourcefulness, but regex really is the right way to go here. This way is too brittle, and relies on some specific things that might break if the string were slightly different. – random_user_name May 07 '18 at 21:08
  • agreed regex is the simpler way to do it :-) upvoted your answer – Eby Jacob May 07 '18 at 21:09