php - Explode string with '/' and escape if it contains space around '/', like ' / ' -


how possible explode string using '/', if slash doesn't contain spaces before or after.

i wish string "flat visors / fitted caps" treated string , "flat visors/fitted caps" explode using '/'.

you can use preg_split negative look-behind , negative look-ahead:

preg_split('/(?<!\s)\\/(?!\s)/', $str); 

if wish opposite, i.e. splitting when there space around , remove space, again preg_split , optional space:

preg_split('/\s*\\/\s*/', $str); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -