Hello, I have data that is stored as a string. They are separated by the symbol "," For example,

name - 'Новострой' add_options - 'Сдан, Не сдан'. 

How can you divide the values ​​in add_options so that you get 2 values:

 1 Здан 2 Не сдан 

And then there was an opportunity to use them in select ??

  • explode(',',$add_options); - Naumov

2 answers 2

Use the explode function :

 $add_options = "Сдан, Не сдан"; $add_options = explode(',', $add_options); foreach($add_options as $option){ echo $option.'<br>'; } 
  • do not forget to trim , for explode explode not remove the space after the comma itself - Alexey Shimansky

Better via preg_split to remove spaces immediately.

 $add_options = "Сдан, Не сдан"; $add_options = preg_split("/\s*,\s*/", $add_options); 

UPD In the comments correctly corrected. Thank you @Firepro

  • believe better explode trim than preg_split - Naumov
  • I'd love to hear the better - OotzlyGootzly
  • read the algorithms, explode bypasses the string once, and preg_split bypasses depending on the mask ... So explode is better and faster. - Naumov
  • one
    explode will be faster on a large sample, there is no difference here. @OotzlyGootzly your example will return 3 lines, instead of the necessary two. - Firepro
  • one
    Ах да, пройтись через for, ведь он быстрей foreach even $array = array_map('trim', $array); ; D - Alexey Shimansky