etrader Posted May 23, 2011 Share Posted May 23, 2011 How I can explode by a series of characters (not one single character)? For example, divide the string to an array by any of these characters "," and ";" and "." $string = "this, that;some.word;second"; I want to put all of these words into an array. Quote Link to comment https://forums.phpfreaks.com/topic/237198-explode-an-string-by-some-characters/ Share on other sites More sharing options...
dragon_sa Posted May 23, 2011 Share Posted May 23, 2011 <?php $string = "this, that;some.word;second"; $change=array(":", ";", ",", "-"); $values=str_replace($change, ".", $string); $result=explode(".", $values); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237198-explode-an-string-by-some-characters/#findComment-1219037 Share on other sites More sharing options...
sasa Posted May 23, 2011 Share Posted May 23, 2011 or <?php $string = "this, that;some.word;second"; $separators = array(":", ";", ",", "-", '\.');//must escape dot $result = preg_split('/('.implode('|',$separators).')\s*/',$string); print_r($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237198-explode-an-string-by-some-characters/#findComment-1219046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.