PHP — Line break affect how regular expression work

As you know,regular expression is very import in text processing.I had never noticed the “link break” in the regular expression.when i matched my “multiple line” regular expression against the string,i found that the line break in both the regular expression and the string to match will affect the match result.below is the detail.

PCRE(Perl-Compatible Regular Expression) for example,let’s take a close look at two special pattern modifier,below is the explanation from php manual document for both PCRE_MULTILINE and PCRE_DOTALL modifier:

By default, PCRE treats the subject string as consisting of a single “line” of characters (even if it actually contains several newlines). The “start of line” metacharacter (^) matches only at the start of the string, while the “end of line” metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the “start of line” and “end of line” constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl’s /m modifier. If there are no “\n” characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl’s /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

so regular expression can match multiple line string and it will be affected by m and s pattern modifier.we do not need to define a regular expression in multiple line like this,thought it’s ok in syntax:

$pattern = “/\| Domain: .*$domainstr.*
\| Expired domain: .*
\| Ip: \d+\.\d+\.\d+\.\d+
\| UserName: .*
\| PassWord: .*

however,we can rewrite it in this way:

$pattern = “/\| Domain: .*$domainstr.*(\r)?\n(\| Expired domain: .*?(\r)?\n)?\| Ip: \d+\.\d+\.\d+\.\d+(\s\(n\))?(\r)?\n(\| HasCgi: y(\r)?\n)?\| UserName: .*(\r)?\n\| PassWord: .*/i”;

The line break depends on it will affect the match we need to pay more attention when define the regular expression.

magento — magento update failed lead to Fatal error: Class ‘Mage_Core_Helper_Hint’ not found

one of my customer ask me to help to fix his prob.he tried to updated magento 1.6.2 to magento 1.7. After that,he logged into the magento backend,and got the following error:

Fatal error: Class ‘Mage_Core_Helper_Hint’ not found

it was obviously that the update process was interrupted due to some problem,Mage_Core_Helper_Hint class was added on mag1.7,just upload the file(Mage/Core/Helper/Hint) that defined the class and the error get fixed.but we may need to check if there are some other missing file.

zen cart — simple script to limit access to zen cart backend to special ip(s)

The script is to limit the access to the specifed ips.

* limit zen cart admin backend only available to specified ips
* @author george zheng <>
* @date 09/05/2012

if ($_SERVER[‘REMOTE_ADDR’] != $ip) {
die(‘You are not permitted to admin backend’);

if (count($ips) && !in_array($_SERVER[‘REMOTE_ADDR’],$ips)) {
die(‘You are not permitted to admin backend’);

zen cart — clear weird char from url of product and cateogry page

with the help of ‘Ultimate SEO URLs’ module,zen cart can generate search engine friendly urls. Though this module do have a feature to replace specified char in the generated url, nobody can set all weird character one by one. it may take a lot time. below is a patch to the ‘Ultimate SEO URLs’ module,which will clear all none ASCII char.

edit the inlcudes/classes/seo.url.php file, add the following line to the function stock_href_link before the switch block:

//fix by george to remove weird char
$link = preg_replace(‘/[^(\x20−\x7F)\x0A]*/’,”, $link);
//end of george fix