Gadget wrote:
Code:
while(preg_match("/([b-df-hj-np-tv-z])(\\1)+/", $string)) {
$string = preg_replace("/([b-df-hj-np-tv-z])(\\1)+/", "\${1}", $string);}
What is the (\\1) doing? Does that mean repeat the first regex?
Yeah, it's a match for the same character as the previous regex. If you omitted it and just repeated the range it could match any 2 consonants and a + would do that as well. I found that the \\1 was necesarry. The need for the \\ is to escape the \.
Gadget wrote:
Here's a tip - may help a little. To prevent having to use two slightly different regexs for the first/last character special cases (you have three examples of this - here is one of them)...
$string = preg_replace("/^x/", "z", $string);
$string = preg_replace("/ x/", " z", $string);
You can often add an extra space to the front and end of the string, do the single regex's, then remove the extra space at the end. YMMV.
I could have simplified it into:
$string = preg_replace("/(^| )x/", "z", $string);
But I didn't have time to test it so I left it as 2 lines.
Gadget wrote:
Does preg stand for perl regular expression? I hope.

Sure does.
Gadget wrote:
My only complaint, and this just may be the way php folks do things and I'm not used to it, is that the while statements seem to be 'unindented' (?) a space and the ending } is not on a seperate line. I had to hunt a bit to find the end of each while loop. At this point, I think you're strong contender for most concise, might have a shot at best readability (although my money is on Manta at this point), and possibly slowest...
Nah, I just didn't clean up the indentions. Normally I do, but I was in a rush as I had to head to a client's office today.
Gadget wrote:
Feedback....
Was that problem too easy? I wanted to make our first little contest problem / language comparison fairly easy, but I'm not too sure if it was too easy, or maybe, a bit too hard for some of the newer folks around here.
I think it was fun. The double consonant regex gave me a little trouble since my regex is a little rusty. Made me feel like I was back in school and I enjoyed it.
P.S. I accidentally removed the white space from the pac man line before feeding it into the program. Here is cleaned up code:
Code:
<?php
function stringcheese($string) {
echo "Original:<br>".$string."<br>";
$string = preg_replace("/^x/", "z", $string);
$string = preg_replace("/ x/", " z", $string);
$string = str_replace("x", "ks", $string);
$string = str_replace("y", "i", $string);
$string = str_replace("ce", "se", $string);
$string = str_replace("ci", "si", $string);
while(preg_match("/c+k/", $string)) {
$string = preg_replace("/c+k/", "k", $string);
}
$string = preg_replace("/^sch/", "sk", $string);
$string = preg_replace("/ sch/", " sk", $string);
$string = preg_replace("/chr/", "kr", $string);
$string = preg_replace("/c[a-gi-z]/", "k", $string);
$string = preg_replace("/c /", "k ", $string);
$string = preg_replace("/^kn/", "n", $string);
$string = preg_replace("/ kn/", " n", $string);
while(preg_match("/([b-df-hj-np-tv-z])(\\1)+/", $string)) {
$string = preg_replace("/([b-df-hj-np-tv-z])(\\1)+/", "\${1}", $string);
}
echo "Result:<br>"$string"<br><br>";
}
$ex1 = "sch kn x xschrx cknnchc cyck xxceci";
$ex2 = " pac man roccccccks ";
$string1 = "i love string cheese!";
$string2 = "please, I want need some cheese";
$string3 = "did you know that Macy's is running a cheese sale?";
$string4 = "xtemex cheedar chexx is for serious cheese snackers";
stringcheese($ex1);
stringcheese($ex2);
stringcheese($string1);
stringcheese($string2);
stringcheese($string3);
stringcheese($string4);
?>