views:

85

answers:

6
$value='x-Cem-Date:Wed, 16 Dec 2009 15:42:28 GMT';

Right now I have:

$value = preg_replace('/(^.+)(?=:)/e', "strtolower('\\1')", $value);

this outputs

$value='x-cem-date:wed, 16 dec 2009 15:42:28 GMT';

it should output:

$value='x-cem-date:Wed, 16 Dec 2009 15:42:28 GMT';
+2  A: 

You might consider using explode() and implode() instead of a regular expression.

$value_a = explode( ':', $value );
$value_a[0] = strtolower( $value_a[0] );
$value = implode( ':', $value_a );
sakabako
Considered that, but in this case I'd prefer regular expressions
stormist
This doesn’t test if there is a colon at all.
Gumbo
@stormist - why?
Skilldrick
In this case its a series of regular expressions that is applied to the string. I'm trying to be consistent in the design instead of doing a series of regular expressions then using explode.
stormist
@gumbo - OP never said what should happen if there is no colon.
sakabako
+3  A: 

Your regular expression should be as follows:

/(^.+?)(?=:)/

The difference is the +? character. The +? is non-greedy, meaning that it will find the LEAST amount of characters until the expression moves onto the next match in the expression, instead of the MOST characters until the next match.

Jeff Rupert
With the `e`, of course. I dropped that because my RegEx parser doesn't work with the Global, Case Insensitive, Extended, etc. modifiers ON the regular expression.
Jeff Rupert
what about the e at the end?
stormist
Got it.. Which RegEx parser do you use?
stormist
I use RegExr. It's a Flash app that I've found really handy for doing Regular Expressions. It can do Match and Replace pretty effectively. Also, I didn't use `e` when doing the expression. I don't know if that changes it or not, but it found the pattern correctly.
Jeff Rupert
http://www.gskinner.com/RegExr/ - That's an online version. Let me see if I can find the standalone app.
Jeff Rupert
Thanks for that, I'll probably use that in the future. For others, the link is here: http://www.gskinner.com/RegExr/
stormist
http://www.gskinner.com/RegExr/desktop/ - If you want to install it on your machine. This is what I use.
Jeff Rupert
+1  A: 

Try

preg_replace('/([\w-]+?)(:[\w\d\s\:\,]+)/e', "strtolower('\\1') . '\\2'", $value);

It works on the example you posted, at least.

Atli
+1  A: 

Just for information, this is the version using preg_replace_callback

$value='x-Cem-Date:Wed, 16 Dec 2009 15:42:28 GMT';

function callback($text){return(strtolower($text[0]));}

echo preg_replace_callback("/^([^:]+:)/","callback",$value);

output

x-cem-date:Wed, 16 Dec 2009 15:42:28 GMT
S.Mark
+1 For using `[^:]+` instead of something else.
Gumbo
"lamb" is not lambda.
stereofrog
@stereofrog, you probably right, but in some languages I understand, those kind of things called lambda functions, in php may be its call callback, lamb is name of the function I called. :-)
S.Mark
+1  A: 
echo preg_replace('~^[^:]+~e', 'strtolower("$0")', $value);
stereofrog
+1  A: 

Try your regular expression with a match

$value='x-Cem-Date:Wed, 16 Dec 2009 15:42:28 GMT';
$value = preg_match('/(^.+)(?=:)/e', $value, $matches); 
print_r ($matches) . "\n";

This should output

Array
(
 [0] => x-Cem-Date:Wed, 16 Dec 2009 15:42
 [1] => x-Cem-Date:Wed, 16 Dec 2009 15:42
)

Try this instead

$value='x-Cem-Date:Wed, 16 Dec 2009 15:42:28 GMT';
$value = preg_replace('/(^.+?:)/e', "strtolower('\\1')", $value); 
echo $value . "\n";

The ? is in there so the regex isn't greedy and grabbing more than it should.

Alan Storm