j.reyes093 Posted July 21, 2010 Share Posted July 21, 2010 I was tasked the other day with formatting about 800 unformatted date fields in a mysql database. Obviously this would be tedious to do by hand but it is tedious as well to do by code (there is an extreme amount of inconsistency in formatting per field). The unformatted area I am dealing with is set up like this (with vast inconsistency within fields): Time1 Time2 Time3 Time4 Time5 Time6 Time7 Time8 Day: xx:xx x.x, xx:xx Day-Day: xx:xx x.x [/td] [td] The general setup can be described with this regular expression: (\*)* *((((mon|tues?|wed(nes)?|thu(r?(?<=r)(s?))*|fri|sat|sun)(?(?!day)(\.?))(day)?)|Holy Day|Vigil|Holy Day/Vigil|Vigil/Holy Day|Weekend|Weekdays||||) *,?-?\**:* *)+ *( *(\*)*((1[0-2])|0?[0-9])(:[0-5][0-9])? *(([A|P|a|p]\.?[M|m]\.)|([A|P|a|p]\.?[M|m]))? *) *,*)+ I only learned PHP a week and a half ago (I know several other languages, I picked up on it quickly) so I am still unaware of all the variety of functions available to me. However, I pushed forward learning as I went. As a result, the code is cleaner and makes more sense the farther you get down the script. I realize (now, at least) something like this could be pretty easily coded with regular expressions, however, I did not learn about them until halfway through the script. I finished the script with them, but the beginning simply compares strings. I would like to not have to redo this entire area with regular expressions if possible. Not surprisingly, the area I am having trouble with is the strings area. A general flow of the code (using english) is: Loop from column 1-8 Loop until there are no more rows call reading script end loop print suggestion list of unrecognized conditions reading script: replace problem strings within field find asterisks and pull corresponding data from original table replace commas with colons and count colons look between each colon and check for specified conditions if there is a specified condition, remove it, log it, and continue looking for conditions .... (the rest works) end The problem is that the script only recognizes one condition per field, and then no longer accepts any more conditions. Here is the relevant code: Variables defined before: (let me know if I missed any) $c_string: current string, the entire field (somewhat formatted in previous code) $acc_day: array containing accepted formats of the days of the week $num_colon = 0; $c = 0; while(strpos($c_string,":",$c)>0 && $num_colon<20) { $colon_pos[$num_colon] = strpos($c_string,":",$c); $c = $colon_pos[$num_colon]+1; $num_colon++; echo "."; } //DETERMINATION OF CONDITIONS AND TIME if($num_colon > 0) { for($c=0,$c1=0;$c<$num_colon;$c++) { //Use $sep_cond[$c] for a list of what was checked in order if(isset($colon_pos[$c-1])) { $sep_cond[$c] = substr($c_string,$c1,$colon_pos[$c]-$colon_pos[$c-1]-1); } else { $sep_cond[$c] = substr($c_string,$c1,$colon_pos[$c]-$colon_pos[$c-1]); } for($d=0;$acc_day[$d];$d++) { if($sep_cond[$c]==$acc_day[$d]) { //PROBLEM AREA: $con_flag_type = "day"; echo $con_flag_type; break; } } if(!isset($con_flag_type)) { //Check for Specific conditioon match for($d=0;$acc_con[$d];$d++) { if($sep_cond[$c] == $acc_con[$d]) { $con_flag_type = "specific"; echo $con_flag_type; break; } } } if(isset($con_flag_type)) { echo "<BR><font size=-2>".$sep_cond[$c]." was determined to be a ".$con_flag_type." condition. Moving on..."; $for_conditions[$conditions_count] = $sep_cond[$c]; $conditions_count++; $c_string = substr($c_string,$colon_pos[$c]+1,strlen($c_string)-$colon_pos[$c]+1); $d=0; $c_pos = $colon_pos[$c]; while(isset($colon_pos[$d])) { $colon_pos[$d]=$colon_pos[$d]-$c_pos-1; $d++; } $c1=0; } else { echo "<BR><font size=-2>".$sep_cond[$c]." was determined to not be a condition.</font>"; $c1 = $colon_pos[$c]+1; } unset($con_flag_type); } Here is an example field that has problem that I have echoed. As you can see, there is a problem. I know that the are marked "PROBLEM AREA" is the problem because I have narrowed it down. I have done an echo of sep_cond[$c] and acc_day[$c] echo $sep_cond[$c]." vs ".$acc_day[$d]; right before the IF statement and in context of the picture, the output reads "Thurs vs Thurs" for the relevant area. So basically, what's wrong with this? if $acc_day[$d] contains all possible days of the week (including different formats of them) $con_flag_type is unset $sep_cond[$c] is "Thurs" for($d=0;$acc_day[$d];$d++) { if($sep_cond[$c]==$acc_day[$d]) { $con_flag_type = "day"; echo $con_flag_type; break; } } Any help would be amazing. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/ Share on other sites More sharing options...
Pikachu2000 Posted July 21, 2010 Share Posted July 21, 2010 Is there any possibility of whitespace being present before/after either value? if( trim($sep_cond[$c]) == trim($acc_day[$d]) ) { Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089349 Share on other sites More sharing options...
j.reyes093 Posted July 22, 2010 Author Share Posted July 22, 2010 I don't think so. Whitespace would show up in an echo, correct? and if I switch the original entry c_string from Fri: 7:00PM Thurs: 630PM to Thurs: 630PM Fri: 7:00PM then I receive that same problem on the Friday, the Thurs: works fine. So i don't believe that's the case. The struggle I'm having here is that since it only works the second time around, one would think the if(isset($con_flag_type))) area would be causing the problem, as that is executed after the first condition is found. However, it would have to affect acc_day or sep_cond as well, and it doesn't do that. But regardless, before the if statement the strings are equal according to echoes; yet, the if statement only checks for equality. So they are equal when echoed, but somehow they're not actually equal. Confusing. I tried using Preg as well to check for equality, same issue then. Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089611 Share on other sites More sharing options...
DavidAM Posted July 22, 2010 Share Posted July 22, 2010 Actually, whitespace might not be so easy to see. If you are running in a browser it will consolidate spaces. If you are running from the command line, it would depend on the font your are using. Your image looks like a proportional space font which would make it hard to determine if there is a space. You could try using var_dump() instead of echo (just before the if). It will show you the datatype, length, and value of the variable, and might give some insight into why it is not working. var_dump(sep_cond[$c]); var_dump(acc_day[$d]); Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089685 Share on other sites More sharing options...
AbraCadaver Posted July 22, 2010 Share Posted July 22, 2010 but some strings are more equal than others ... Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089691 Share on other sites More sharing options...
j.reyes093 Posted July 22, 2010 Author Share Posted July 22, 2010 Wow thanks, vardump worked! I didn't realize the browser would just eliminate a space like that. Kind of annoying for developers. I have it working now, just a couple of small bugs and I'm done. but some strings are more equal than others ... I want to laugh at this because if you replace "strings" with people sounds like an allusion to something from the civil rights era. But I'm new to php, so you could be entirely serious for all I know. Please excuse my stupidity Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089724 Share on other sites More sharing options...
Pikachu2000 Posted July 22, 2010 Share Posted July 22, 2010 but some strings are more equal than others ... I want to laugh at this because if you replace "strings" with people sounds like an allusion to something from the civil rights era. But I'm new to php, so you could be entirely serious for all I know. Please excuse my stupidity [Archie Bunker] Equal to me? You ain't even equal to him! [/Archie Bunker] Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089725 Share on other sites More sharing options...
AbraCadaver Posted July 22, 2010 Share Posted July 22, 2010 I want to laugh at this because if you replace "strings" with people sounds like an allusion to something from the civil rights era. But I'm new to php, so you could be entirely serious for all I know. Please excuse my stupidity http://en.wikipedia.org/wiki/Animal_Farm Quote Link to comment https://forums.phpfreaks.com/topic/208468-equal-strings-are-not-being-treated-equal/#findComment-1089758 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.