2

I have php regex to find tag and extract css address from html page

'/<link.*?href\s*=\s*["\']([^"\']+)[^>]*>.*?\/>/i'

but it doesn't work good.can you help me to modify this code?

3 Answers3

0

Perhaps

'/<link .*?(href=[\'|"](.*)?[\'|"]|\/?\>)/i'

Then you can acces the link with $2

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
0

Regex to find hrefs of all stylesheets can be a tricky task. You should consider using some PHP HTML parser to get this information.

You can read this article to get more information and then try this code.

// Retrieve all links and print their HREFs
foreach($html->find('link') as $e)
    echo $e->href . '<br>';

// Retrieve all script tags and print their SRCs
foreach($html->find('script') as $e)
    echo $e->src . '<br>';

PS: Remember, your script tag may not contain a src then it will print empty string.

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
0

Not that this is better than the other answer, however just in case you want to see it, I've altered your regex such that it should work as intended:

'/<link.*?href\s*=\s*["\']([^"\']+?)[\'"]/i'
OGHaza
  • 4,795
  • 7
  • 23
  • 29