Yammyguy Posted April 15, 2009 Share Posted April 15, 2009 Hello, here's a simple issue I'm having. I have an array with 4 values as follows: Array ( [0] => " KW " [1] => " KVAR " [2] => " " [3] => " " ) I want to strip the double quotes from each value, and count how many array cells actually contain an actual value once the quotes are removed. so I did this: $used = 0; for ($x = 0; $x < $4; $x++){ $header_data[$x] = trim($header_data[$x], '"'); if($header_data[$x] !== ""){ $used++; } } print_r($header_data); echo $used; I thought this was very straight forward - apparently not! :S here's my print_r of that array: Array ( [0] => KW [1] => KVAR [2] => [3] => " ) and the output for my $used variable is "0" First of all, for some reason, this will not remove the last double quote in the last array cell?? I'm so confused? I don't get it. And second, am I using the correct syntax for checking a null value in an array? Thanks in advance for any suggestions, or help! And if you have a more efficient way of performing this task, feel free to post! CHEERS! ~C. Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2009 Share Posted April 15, 2009 The last array element probably has a new-line after the second ". Try var_dump instead of print_r as var_dump will also show the length of the data. Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-810962 Share on other sites More sharing options...
Yammyguy Posted April 15, 2009 Author Share Posted April 15, 2009 here's my output from the var_dump() array(4) { [0]=> string(7) " KW " [1]=> string(7) " KVAR " [2]=> string(7) " " [3]=> string(169) " " " } obviously there's a third quote (which is really odd) - but the trim should remove every instance of the double quote, no? What pray tel, would you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-810967 Share on other sites More sharing options...
sasa Posted April 15, 2009 Share Posted April 15, 2009 <?php $header_data = array ( 0 => '" KW "', 1 => '" KVAR "', 2 => '" "', 3 => '" "' ); $used = 0; for ($x = 0; $x < 4; $x++){ $header_data[$x] = trim($header_data[$x], ' "'); if($header_data[$x] != ""){ $used++; } } print_r($header_data); echo $used; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-810968 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2009 Share Posted April 15, 2009 Based on the lengths of the strings, you have a significant number of non-printing characters in the data (all elements). Either nulls, new-lines or something else... Trim only trims from the start and end of the string. When it encounters a character not in the list of characters to be trimmed it stops. You probably want str_replace instead of trim and you may in fact want to remove all non-alphabetic characters. Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-810973 Share on other sites More sharing options...
Yammyguy Posted April 15, 2009 Author Share Posted April 15, 2009 Based on the lengths of the strings, you have a significant number of non-printing characters in the data (all elements). Either nulls, new-lines or something else... Trim only trims from the start and end of the string. When it encounters a character not in the list of characters to be trimmed it stops. You probably want str_replace instead of trim and you may in fact want to remove all non-alphabetic characters. Thanks for the suggestions. I'll work on those changes and let you know if I encounter any problems. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-810976 Share on other sites More sharing options...
Yammyguy Posted April 15, 2009 Author Share Posted April 15, 2009 Okay - I've replaced the str_replace by inserting the following code: $used = 0; for ($x = 0; $x < $4; $x++){ #new code line here# $header_data[$x] = str_replace('"',"",$header_data[$x]); #end new code# //$header_data[$x] = trim($header_data[$x], '"'); if($header_data[$x] !== ""){ $used++; } } print_r($header_data); echo $used; Doesn't really work out - my problem is that I can't figure out how to remove everything but letters in each of the array. If anyone can help - I must say I'm still stuck. Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-811051 Share on other sites More sharing options...
premiso Posted April 16, 2009 Share Posted April 16, 2009 <?php $header_data[] = "testtest1234_'\""; $header_data[] = "testtest5678-;:[].,<>?_'\"adsf"; $used = 0; for ($x = 0; $x < 2; $x++){ #new code line here# $header_data[$x] = preg_replace('~([^a-z]+)~i',"",$header_data[$x]); #end new code# //$header_data[$x] = trim($header_data[$x], '"'); if($header_data[$x] !== ""){ $used++; } } echo "<pre>"; var_dump($header_data); echo $used; die(); ?> Output: array(2) { [0]=> string( "testtest" [1]=> string(12) "testtestadsf" } 2 Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-811072 Share on other sites More sharing options...
Yammyguy Posted April 16, 2009 Author Share Posted April 16, 2009 Thanks! as soon as I tried the preg_replace() function - you posted... I have one quick question though, is the '~([^a-z]+)~i' portion of the preg_replace() a specific structure? I don't think I understand how entering those values will know to remove everything :-\ Not sure I'm explaining my question properly... ??? Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-811073 Share on other sites More sharing options...
premiso Posted April 16, 2009 Share Posted April 16, 2009 The ~ just states everything in between is part of the regex. The ( ) is the conditional. Inside the [ ] is the characters we want to test. If the item is not a-z it gets replaced the ^ before the a-z indicates not. The + says match 1 (it may be 0) or more characters that are not a-z. If it finds the match then it replaces it. Regex can be complicated and take some time to understand, but is very powerful and useful. EDIT: To any regex experts, if what I have stated is wrong, please correct me I am still learning regex. Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-811074 Share on other sites More sharing options...
Yammyguy Posted April 16, 2009 Author Share Posted April 16, 2009 The ~ just states everything in between is part of the regex. The ( ) is the conditional. Inside the [ ] is the characters we want to test. If the item is not a-z it gets replaced the ^ before the a-z indicates not. The + says match 1 (it may be 0) or more characters that are not a-z. If it finds the match then it replaces it. Regex can be complicated and take some time to understand, but is very powerful and useful. Awesome! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/154257-solved-trim-not-trimming-everything/#findComment-811076 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.