Double Quotes not matching in (Cake)PHP Regex
I have a regular expression that should be matching some string of the form: src="/blkjad.dafsdf">. It looks like this: (src|href)(\s*)?=(\s*)?(\"|\')/([^/].*?)\4.
While this works fine on one site I'm running, it fails consistently on another, built in Cake. Here's another failing Regex => '/(")/' which should be matching any double quotation in the line.
I'm at a loss right now. One of the senior developers where I work just took a look at it and basically shook his head. Unfortunately, I'm not that proficient with Cake, and I can't seem to find any similar problems online. Any ideas?
edit: For the record, the failing regex uses preg_match_all, while the other (identical/ working regex) uses preg_replace. Here is some relevant code.
$regex['php'] = '/(src|href)(\s*)?=(\s*)?(\"|\')\/([^\/].*?)\4/';
$php = htmlspecialchars(file_get_contents(sprintf('/home/x/x/x/x/x/%s.%s', $string, $extension))); echo $php;
if (preg_match_all($regex['php'], $php, $out)){
echo sizeof($out);
echo nl2br(print_r($out, true));
} else {
echo 'Doesn\开发者_JAVA技巧't work';
}
I think the problem comes from the htmlspecialchars() call.
As stated in the PHP documentation
The translations performed are:
- '&' (ampersand) becomes '&'
- '"' (double quote) becomes '"'
- when ENT_NOQUOTES is not set. "'" (single quote) becomes ''' only
- when ENT_QUOTES is set. '<' (less than) becomes '<' '>' (greater than) becomes '>'
精彩评论