Dynamo
6 replies to this thread. Most Recent
Paul
22 May 2008, 9:47 am
Regular Expressions
HI, I have a regular expression which works well as GREP in BBEdit, but I can’t get the thing to work in PHP. When I run this:
$myData = “Item Name,W-0194,toy,W-0194-toy,1.75,,0.80r”; $pattern = “#(.?),(.?),(.?),(.?),(.?),(.?)r#”; $replacement = “INSERT INTO fiesta_spares ( part_name, product_code, part_description, part_no , price , stockLevel ) VALUES ( ‘1’, ‘2’, ‘3’, ‘4’ , ‘5’ , ‘100’ );r”;
$myData = ereg_replace($pattern,$replacement,$myData); echo $myData;
$myData is unchanged. The escaped characters in $pattern are not in the GREP in BBEdit (unescaped, the thing fails with an error).
I hate regular expressions…
thatkeith
22 May 2008, 9:58 amSometime around 22/5/08 (at 05:47 -0400) Paul said:
I hate regular expressions…
Regex: Powerful. Painful. Makes my head hurt.
k
Paul
22 May 2008, 10:25 amArrggggg…… and the forum code has stripped out all the escape characters from the example I posted!!
That noise - hear it? It’s me. Grinding my teeth. again.
waltd
22 May 2008, 12:25 pmSorry about the backslashing, use Pastie for this, I really will work on this as soon as I get a chance. It’s very frustrating, because the live preview gets it right, and the same function is used in both cases. I suspect it has something to do with magic_quotes_gpc, which is just a little too voodoo in some cases.
Also, ereg_replace is not as Perl-compatible as preg_replace — if something works in BBEdit, and doesn’t in ereg, then try preg. Note that you have to be very Perl-y with your regex string in preg, ereg is looser.
Walter
Freeway user since 1997
waltd
22 May 2008, 2:06 pmPaul, looking at your input string, it would appear that you are trying to parse a CSV file with a regular expression. Could you not do this to cast it into an array?
$properties = preg_split('/,/',$inputString,-1,PREG_SPLIT_NO_EMPTY);
And then blast the fields into your query like this:
$sql = 'INSERT INTO fiesta_spares ( part_name,
product_code,
part_description,
part_no,
price,
stockLevel )
VALUES ( "' . implode('","',$properties) . '")';
Unless your data fields can contain commas (can’t tell from your example code) then this should just work. Much less mind-bending than trying to grab the indexed properties one at a time inside a regex.
Walter
Freeway user since 1997
Joe Billings
22 May 2008, 2:27 pmYeah it looks like you are doing straight replacement rather than looking for specific patterns, I agree with Walter that tokenizing the string and then replacing the nth token may be the best option (if that’s what you are wanting to do).
Joe
On 22 May 2008, at 10:47, Paul wrote:
HI, I have a regular expression which works well as GREP in BBEdit, but I cant get the thing to work in PHP. When I run this:
$myData = “Item Name,W-0194,toy,W-0194-toy,1.75,,0.80r”; $pattern = “#(.?),(.?),(.?),(.?),(.?),(.?)r#”; $replacement = “INSERT INTO fiesta_spares ( part_name, product_code, part_description, part_no , price , stockLevel ) VALUES ( ‘1’, ’ 2’, ‘3’, ‘4’ , ‘5’ , ‘100’ );r”;
$myData = ereg_replace($pattern,$replacement,$myData); echo $myData;$myData is unchanged. The escaped characters in $pattern are not in the GREP in BBEdit (unescaped, the thing fails with an error).
I hate regular expressions
For free and responsive Freeway support visit www.softpress.com/support/help_desk.php