webent Posted August 22, 2008 Share Posted August 22, 2008 Hi, I was hoping someone might be able to tell me if the best way to split a string into multiple strings is to use split? I have read that using preg_split, str_split, and explode are often considered better... I am importing a csv file and one field in particular uses "|" to separate the categories... I need to be able to take the first section and make it say $var1, the second part, if there is one, and make it $var2, and the third part, if there is one, $var3 and throw away any that may follow... Here is one of the field entries for instance, "consumer electronics|phone and telecom|headsets" So say, $CATEGORIES = "consumer electronics|phone and telecom|headsets"; list($var1, $var2, $var3) = split('[|]', $CATEGORIES); Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/ Share on other sites More sharing options...
ratcateme Posted August 22, 2008 Share Posted August 22, 2008 that looks file i don't think in this kind of application with only 3 parts you would get any better performance from one of the other functions Scott. Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622773 Share on other sites More sharing options...
Zane Posted August 22, 2008 Share Posted August 22, 2008 I don't understand why you would want all those variable when you could just have a full array with keys but two ways I can come up with are 1. Explode and Extract //explode the the categories $CATEGORIES = "consumer electronics|phone and telecom|headsets"; $cats = explode("|",$cats); //then extract them all with a prefix "var" extract($cats, "EXTR_PREFIX_ALL", "var"); 2. Explode and Foreach it //explode the categories $CATEGORIES = "consumer electronics|phone and telecom|headsets"; $cats = explode("|",$cats); //then loop through and create each variable $i = 1; foreach($cats as $cat) { $var{$i} = $cat; $i++; } The first one will make variables like this $var_x Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622780 Share on other sites More sharing options...
ratcateme Posted August 22, 2008 Share Posted August 22, 2008 what is wrong with his code? Scott. Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622783 Share on other sites More sharing options...
Zane Posted August 22, 2008 Share Posted August 22, 2008 I never really said anything was wrong. I don't even think I clearly understood the question. If the question was.............. is your code good, then yes. but you could probably just explode instead of split. Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622784 Share on other sites More sharing options...
webent Posted August 22, 2008 Author Share Posted August 22, 2008 I'm sorry if there was any confusion as to what I'm trying to do... I have to insert the split strings into the database under, product_line, product_master_category, and product_category fields... Thank you all for your responses Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622923 Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Just use explode() instead of split(). From the manual: If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine. Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622939 Share on other sites More sharing options...
webent Posted August 22, 2008 Author Share Posted August 22, 2008 Very good, I will do, thank you guys/gals for all of your help, I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/120810-solved-splitting-strings/#findComment-622949 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.