1
private function pageScrape( $url )
{
    $page_stream = file_get_contents( $url );
    $pattern = '/<link\s+(?=[^>]*rel="(?:[Ss]hortcut\s)?[Ii]con"\s+)(?:[^>]*href="(.+?)").*/>/';
    preg_match( $pattern, $page_stream, $matches );
    print_r( $matches );
    // echo $page_stream;
}

gives error:

Warning: preg_match() [function.preg-match]: Unknown modifier '>' in /home/foo/public_html/foo/source/class.ControlBookmark.php on line 16

PHP.net reference on pcre

http://php.net/manual/en/reference.pcre.pattern.syntax.php

2 Answers2

2

Use regex boundaries/delimiter in your regex pattern variable $pattern like this:

$pattern = '#<link\s+(?=[^>]*rel="(?:[Ss]hortcut\s)?[Ii]con"\s+)(?:[^>]*href="(.+?)").*/>#';
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I like forward slashes for consistency. –  May 23 '12 at 22:09
  • 1
    Since your regex already has / (forward slash) it is better to avoid using / as regex delimiter (like in my answer using #) or else escape it like this \/. – anubhava May 23 '12 at 22:10
  • In PCRE regex you can use any of these: `/` or `#` or `~` or `|` or `@` or `;` as regex delimiter. – anubhava May 23 '12 at 22:16
  • ...wow..that would explain a lot...I saw someone use ~...and I didn't know what the heck it was....I'll keep that in mind. –  May 23 '12 at 22:18
  • Make not that ~ looks the coolest I think...like a regular expression angel with wings... –  May 25 '12 at 15:51
1

You problem is due to unescaped slash at the end of your expression. Try this instead:

$pattern = '/<link\s+(?=[^>]*rel="(?:[Ss]hortcut\s)?[Ii]con"\s+)(?:[^>]*href="(.+?)").*\/>/';
Aleks G
  • 56,435
  • 29
  • 168
  • 265