Jump to content

[SOLVED] preg_replace help


SharkBait

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.