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

AbotX : How do you create a parallel crawler that stays on and can be added to at run time from new requests -

testing - Detect whether test has failed within fixture -

android - Create single AAR file from multiple modules using Gradle -