SharkBait Posted September 24, 2008 Share Posted September 24, 2008 Ok I've been trying to figure out the use of regex and hit a bit of a snag. i am trying to massage some data and if the string after the comma is larger than 2 characters I want to drop the comma Example: <?php $string = 'My Item, N-female'; $pattern = '\(\w+),(\s+)(\w{3})/i'; $replacement = '$1 $3'; $temp = preg_replace($pattern, $replacement, $string); ?> But I think the issue I am having is with the hyphen and the above pattern will not drop the comma when but when the string is <?php $string = 'My Item, Fun'; ?> Then it does drop the comma. I am trying to make it so that it drop the comma if the characters after the comma are 2 and less. But if the 2nd character is a hyphen it doesn't drop the comma. What do I need to do? Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/ Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 /,(\s+.{2}?)/ $1 <?php $string = 'My Item, N-female'; $pattern = '/,(\s+.{2}?)/s'; $replacement = '$1'; echo preg_replace($pattern, $replacement, $string); ?> Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649564 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 Ok, though if <?php $string = 'My Item, OT'; ?> I don't want to drop the comma. Though if it has a hyphen then i want to drop the comma. Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649574 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 <?php $string = 'My Item, N-female'; $pattern = '/,(\s+.{3}?)/s'; $replacement = '$1'; echo preg_replace($pattern, $replacement, $string); ?> Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649577 Share on other sites More sharing options...
effigy Posted September 24, 2008 Share Posted September 24, 2008 <pre> <?php $string = 'My Item, N-female'; echo preg_replace('/,(?=\s+.{3})/s', '', $string); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649583 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 Ah, forgot about using a positive lookahead. Good call. Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649598 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 Ok I'm slowly getting the idea... perhaps this will help more in explaining what I am trying to do <?php $string = '9/22/2008,Sales,Sales History Ranging From 9/15/2008 To 9/19/2008,Page # 0001,Date,Customer,City & State,ZIP Code Qty, UM,Part Number:,TR-5amp-Nf,"5GHz AP/PtP/CPE, N-female",9/16/2008,"Company. LLC","Nevada, MO",64772,1,EA,Brand Total,799'; ?> I want to drop the comma in the "5GHz AP/PtP/CPE, N-Female" but keep the comma in the "Nevada, MO" so when I explode the string it will hopefully seperate the Nevada and MO only. Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649601 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 Is it that you don't want things in " " being exploded, like a CSV? Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649616 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 I do and I don't. That's the trick. There is a field that is City, State and I need to leave the comma in, but all the other fields i would like to remove the comma from. I can do this manually of course, but the end result is that a persona can upload a file, have it corrected for these issues, then write it to a new file which is then loaded into a MySQL table. Unfortunately 1 field needs to be split, the others do not. its why i was trying to match against 2 characters after the comma because in the data files the state is always 2 characters, anything more and it's not a state (not the right field). Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649623 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 Did you try my revised regex or effigy's? Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649637 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 Yes I've tried both. Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649644 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 <?php $string = '9/22/2008,Sales,Sales History Ranging From 9/15/2008 To 9/19/2008,Page # 0001,Date,Customer,City & State,ZIP Code Qty, UM,Part Number:,TR-5amp-Nf,"5GHz AP/PtP/CPE, N-female",9/16/2008,"Company. LLC","Nevada, MO",64772,1,EA,Brand Total,799'; $pattern = '/,(?=\s+.{3})/'; $replacement = ''; echo preg_replace($pattern, $replacement, $string); ?> That seems to work for me. Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649648 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 weird so it removes the comma from in front of N-Female but leaves it in for the Nevada, MO ?? Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649656 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 Ah, I see the problem. We all wrote out regexes assuming that your sample string was the whole string. You'd need: <?php $string = '9/22/2008,Sales,Sales History Ranging From 9/15/2008 To 9/19/2008,Page # 0001,Date,Customer,City & State,ZIP Code Qty, UM,Part Number:,TR-5amp-Nf,"5GHz AP/PtP/CPE, N-female",9/16/2008,"Company. LLC","Nevada, MO",64772,1,EA,Brand Total,799'; $pattern = '/,(?=\s+[\w\d-]{3})/'; $replacement = ''; echo preg_replace($pattern, $replacement, $string); ?> That actually works as you want (I just tested this one. I tested the other one too, but I forgot to look at the Nevada part of the output. =P) Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649662 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 Ah cool that works nicely. So let me see if I can explain it in english as to what the regex does so i can understand what it does. ?=\s+ if after a comma and whitespace [\w\d-] can be word, numbers or hyphens {3} 3 or above characters [code] right? [/code] Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649674 Share on other sites More sharing options...
DarkWater Posted September 24, 2008 Share Posted September 24, 2008 Actually: / - start regex , - match a single comma (?= - begin positive lookahead \s+ = match one or more space characters after the comma [\w\d-]{3} - match 3 characters in A-Za-z_0-9- ) - end positive lookahead / - end regex Then, since a positive lookahead doesn't actually match anything, you can use '' as the replacement. Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649682 Share on other sites More sharing options...
SharkBait Posted September 24, 2008 Author Share Posted September 24, 2008 Thanks for the help! Much appreciated Link to comment https://forums.phpfreaks.com/topic/125635-solved-preg_replace-help/#findComment-649721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.