View Full Version : PHP regular expression

11-10-2011, 10:19 PM
Hi, I have a question about regular expression.

$ means matching from the end of a string and ^ from the beginning. However, why does the following expression return failure?

$mode = "/^php$/";
$string = "phpphp";
preg_match($mode, $string);

what I think is that both the beginning and end contain 'php', so it should returns 1, but it doesn't.

Thanks a lot,

11-10-2011, 10:26 PM
You're more or less saying "does 'phpphp' == 'php'?"

Here is the literal translation of your regular expression:


Assert position at the beginning of the string «^»
Match the characters “php” literally «php»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
It doesn't match because you're trying to match literal string "php" with "phpphp".