I need to extract all href from external css link e.g. <link rel="stylesheet" href="main.css" type="text/css"> OR <link rel="stylesheet" type="text/css" href="main.css"> OR <link href="main.css" type="text/css" rel="stylesheet" > OR from any of combination of insides attributes. Simply I just need to find all external css location, Expected Result: main.css Pls help
It seems you need a lot of regular expressions, I think it would be very useful to learn them, instead of posting whenever you need one.
^ I agree... Anyway... preg_match_all('~<link[^>]+(?:(?:href="([^"]+)"|type="text/css")\s*){2}[^>]*>~', $html, $matches); echo '<pre>', print_r($matches[1], true), '</pre>'; PHP:
I am agreed too. thanks for help. I think ,I will help others next time when they need reg exp problem. Thanks
Hi, Does not work. Since all pattern seems to same to me, I cant find the error. What the the problems?
I've tried it, and it works just fine for me: $html = '<link rel="stylesheet" href="main3.css" type="text/css"> OR <link rel="stylesheet" type="text/css" href="main5.css"> OR <link href="main4.css" type="text/css" rel="stylesheet" > OR'; preg_match_all('~<link[^>]+(?:(?:href="([^"]+)"|type="text/css")\s*){2}[^>]*>~', $html, $matches); echo '<pre>', print_r($matches[1], true), '</pre>'; PHP: Gives me: Array ( [0] => main3.css [1] => main5.css [2] => main4.css ) Code (markup): How did you try it? That means this group won't be captured in the matches array.
Hi Nicho, My html was as $html='<meta name="Author" content="Bdjokes.com" /> <meta name="Rating" content="General" /> <title>bdjokes :: A collection of jokes in bangla(Bangla Jokes)</title> <link href="css.css" rel="stylesheet" type="text/css"> <link href="style.css" rel="stylesheet" type="text/css"> </head>'; preg_match_all('~<link[^>]+(?:(?:href="([^"]+)"|type="text/css")\s*){2}[^>]*>~', $html, $matches); echo '<pre>', print_r($matches[1], true), '</pre>'; PHP: but no result... Thanks
Works for me: '~<link[^>]+(?:\s*(?:href="([^"]+)"|type="text/css"|rel="stylesheet")\s*){3}[^>]*>~' PHP: