TheJoey Posted October 12, 2009 Share Posted October 12, 2009 hello i have a text file which has information seperated by | so its text1 | text2 | text3 | text4| how do i extract text1 & text2 so i can use it in a login.. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/ Share on other sites More sharing options...
PugJr Posted October 12, 2009 Share Posted October 12, 2009 Like this? $text = "text1 | text2 | text3 | text4|"; $array = explode('|', $text); $piece = $array['0']; /// For 0 = text1, 1 = text2, 2 = text3, 3 = text4, 4 = "", ... Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935255 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 so i would make piece $piece = $array['0']['1']; Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935256 Share on other sites More sharing options...
PugJr Posted October 12, 2009 Share Posted October 12, 2009 so i would make piece $piece = $array['0']['1']; Um...I'm not gonna test if that works or not, but I'm pretty sure it doesn't. You could do: $piece = $array['0']; $piecetwo = $array['1']; $combinepiece = "$pieceone$piecetwo"; ; Unless I'm not understanding you correctly. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935259 Share on other sites More sharing options...
Alex Posted October 12, 2009 Share Posted October 12, 2009 You'd want to use trim() as well to remove the white space (that or explode by ' | ' instead of '|'). There's also no point in surrounding the indexes of the array in single quotes because it's an integer. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935260 Share on other sites More sharing options...
PugJr Posted October 12, 2009 Share Posted October 12, 2009 You'd want to use trim() as well to remove the white space (that or explode by ' | ' instead of '|'). There's also no point in surrounding the indexes of the array in single quotes because it's an integer. Right, well originally I thought (blank) parsed while '' did not parse. And that still seems to be true, but as I tested speed, some reason parsing processed noticeably faster than non-parsed. Would you care to explain to me how this is? Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935267 Share on other sites More sharing options...
Alex Posted October 12, 2009 Share Posted October 12, 2009 I'm not sure what you mean by '(blank)', but integers aren't meant to be used without quotes, so I'd guess that's why it's faster. As for what single quotes parse: Single quotes will parse variables as literals, and not their value. While double quotes will parse them. eg $var = 'Hello'; echo '$var'; // $var echo "$var"; // Hello Note that in that example normally you wouldn't even use "$var", because variables don't require quotes either. It's just an example. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935270 Share on other sites More sharing options...
PugJr Posted October 12, 2009 Share Posted October 12, 2009 I'm not sure what you mean by '(blank)', but integers aren't meant to be used without quotes, so I'd guess that's why it's faster. As for what single quotes parse: Single quotes will parse variables as literals, and not their value. While double quotes will parse them. eg $var = 'Hello'; echo '$var'; // $var echo "$var"; // Hello Note that in that example normally you wouldn't even use "$var", because variables don't require quotes either. It's just an example. But its improper not to have quotes. It comes under as an error. Also I thought PHP auto-assumes that if you are lacking quotes you mean single quotes. Perhaps I am mistaken there as well. Also, why are integers faster without single quotes? Whats the reasoning behind this? I would think that not parsing the numbers would be quicker but clearly I am mistaken. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935274 Share on other sites More sharing options...
Alex Posted October 12, 2009 Share Posted October 12, 2009 It's improper to not have quotes when using strings, although sometimes it won't throw errors (depending on your reporting level). eg (This is improper, but depending on your reporting level won't throw an error) $arr = Array('first' => 'hello', 'second' => 'world'); echo $arr[first]; However not using quotes on integer indexes won't throw an error (It's actually preferred). PHP Processes integers faster than strings, so that's why $arr[0] would be faster than $arr['0']. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935276 Share on other sites More sharing options...
gizmola Posted October 12, 2009 Share Posted October 12, 2009 so i would make piece $piece = $array['0']['1']; No, you get a one dimensional zero based array. So: $piece1 = $array[0]; $piece2 = $array[1]; Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935277 Share on other sites More sharing options...
TheJoey Posted October 12, 2009 Author Share Posted October 12, 2009 where should i apply the trim? Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935278 Share on other sites More sharing options...
PugJr Posted October 12, 2009 Share Posted October 12, 2009 PHP Processes integers faster than strings, so that's why $arr[0] would be faster than $arr['0']. Ah, excellent. That explains it. Thanks for the tid bit. Funny how you can have all these little things which will make your PHP script just a little bit faster at a time. Gizmola, I already corrected that. (Well, excluding the single quotes and no quotes which I just learned from AlexWD.) Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935282 Share on other sites More sharing options...
gizmola Posted October 12, 2009 Share Posted October 12, 2009 It's improper to not have quotes when using strings, although sometimes it won't throw errors (depending on your reporting level). eg (This is improper, but depending on your reporting level won't throw an error) $arr = Array('first' => 'hello', 'second' => 'world'); echo $arr[first]; However not using quotes on integer indexes won't throw an error (It's actually preferred). PHP Processes integers faster than strings, so that's why $arr[0] would be faster than $arr['0']. This really isn't how PHP works. PHP has a hashed symbol table. I doubt there's any noticeble difference, but technically there is probably a little bit more overhead in parsing: $arr[0] compared to $arr['0'] -- that overhead being the need for PHP to check that zero is not a constant. With that said, I would never bother to use $arr['0'] even though internally I know that all array indexes are strings. Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935283 Share on other sites More sharing options...
gizmola Posted October 12, 2009 Share Posted October 12, 2009 where should i apply the trim? $piece1 = trim($array[0]); Quote Link to comment https://forums.phpfreaks.com/topic/177383-using-implode-explode/#findComment-935285 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.