I have the following php code from php.net
<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
I don’t get why they are using ‘/php/i‘ not(‘php‘) for matching. Please help me out. Thanks
This is so called “perl regular expression” syntax, which originated in Perl but commonly used in many languages.
/characters delimit the regular expression, and the lasticharacter is an option modifying matching behavior – in this case, saying that the match should be case insensitive, i.e. match all strings like “PHP”, “php”, “PhP”, etc.You can read more on regular expressions in the PHP manual.
If you’re asking why they don’t use spaces around – that depends on what you want to find. I would recommend using
/\bphp\b/ifor matching whole words.\bmeans “word boundary”.