4 * Copyright (c) 2008, David R. Nadeau, NadeauSoftware.com.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * * Neither the names of David R. Nadeau or NadeauSoftware.com, nor
20 * the names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
34 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
39 * This is a BSD License approved by the Open Source Initiative (OSI).
40 * See: http://www.opensource.org/licenses/bsd-license.php
44 * Combine a base URL and a relative URL to produce a new
45 * absolute URL. The base URL is often the URL of a page,
46 * and the relative URL is a URL embedded on that page.
48 * This function implements the "absolutize" algorithm from
49 * the RFC3986 specification for URLs.
51 * This function supports multi-byte characters with the UTF-8 encoding,
52 * per the URL specification.
55 * baseUrl the absolute base URL.
57 * url the relative URL to convert.
60 * An absolute URL that combines parts of the base and relative
61 * URLs, or FALSE if the base URL is not absolute or if either
62 * URL cannot be parsed.
64 function url_to_absolute( $baseUrl, $relativeUrl )
66 // If relative URL has a scheme, clean path and return.
67 $r = split_url( $relativeUrl );
70 if ( !empty( $r['scheme'] ) )
72 if ( !empty( $r['path'] ) && $r['path'][0] == '/' )
73 $r['path'] = url_remove_dot_segments( $r['path'] );
74 return join_url( $r );
77 // Make sure the base URL is absolute.
78 $b = split_url( $baseUrl );
79 if ( $b === FALSE ||
empty( $b['scheme'] ) ||
empty( $b['host'] ) )
81 $r['scheme'] = $b['scheme'];
83 // If relative URL has an authority, clean path and return.
84 if ( isset( $r['host'] ) )
86 if ( !empty( $r['path'] ) )
87 $r['path'] = url_remove_dot_segments( $r['path'] );
88 return join_url( $r );
94 // Copy base authority.
95 $r['host'] = $b['host'];
96 if ( isset( $b['port'] ) ) $r['port'] = $b['port'];
97 if ( isset( $b['user'] ) ) $r['user'] = $b['user'];
98 if ( isset( $b['pass'] ) ) $r['pass'] = $b['pass'];
100 // If relative URL has no path, use base path
101 if ( empty( $r['path'] ) )
103 if ( !empty( $b['path'] ) )
104 $r['path'] = $b['path'];
105 if ( !isset( $r['query'] ) && isset( $b['query'] ) )
106 $r['query'] = $b['query'];
107 return join_url( $r );
110 // If relative URL path doesn't start with /, merge with base path
111 if ( $r['path'][0] != '/' )
113 $base = mb_strrchr( $b['path'], '/', TRUE, 'UTF-8' );
114 if ( $base === FALSE ) $base = '';
115 $r['path'] = $base . '/' . $r['path'];
117 $r['path'] = url_remove_dot_segments( $r['path'] );
118 return join_url( $r );
122 * Filter out "." and ".." segments from a URL's path and return
125 * This function implements the "remove_dot_segments" algorithm from
126 * the RFC3986 specification for URLs.
128 * This function supports multi-byte characters with the UTF-8 encoding,
129 * per the URL specification.
132 * path the path to filter
135 * The filtered path with "." and ".." removed.
137 function url_remove_dot_segments( $path )
139 // multi-byte character explode
140 $inSegs = preg_split( '!/!u', $path );
142 foreach ( $inSegs as $seg )
144 if ( $seg == '' ||
$seg == '.')
147 array_pop( $outSegs );
149 array_push( $outSegs, $seg );
151 $outPath = implode( '/', $outSegs );
152 if ( $path[0] == '/' )
153 $outPath = '/' . $outPath;
154 // compare last multi-byte character against '/'
155 if ( $outPath != '/' &&
156 (mb_strlen($path)-1) == mb_strrpos( $path, '/', 'UTF-8' ) )
162 * This function parses an absolute or relative URL and splits it
163 * into individual components.
165 * RFC3986 specifies the components of a Uniform Resource Identifier (URI).
166 * A portion of the ABNFs are repeated here:
168 * URI-reference = URI
171 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
173 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
175 * hier-part = "//" authority path-abempty
180 * relative-part = "//" authority path-abempty
185 * authority = [ userinfo "@" ] host [ ":" port ]
187 * So, a URL has the following major components:
190 * The name of a method used to interpret the rest of
191 * the URL. Examples: "http", "https", "mailto", "file'.
194 * The name of the authority governing the URL's name
195 * space. Examples: "example.com", "user@example.com",
196 * "example.com:80", "user:password@example.com:80".
198 * The authority may include a host name, port number,
199 * user name, and password.
201 * The host may be a name, an IPv4 numeric address, or
202 * an IPv6 numeric address.
205 * The hierarchical path to the URL's resource.
206 * Examples: "/index.htm", "/scripts/page.php".
209 * The data for a query. Examples: "?search=google.com".
212 * The name of a secondary resource relative to that named
213 * by the path. Examples: "#section1", "#header".
215 * An "absolute" URL must include a scheme and path. The authority, query,
216 * and fragment components are optional.
218 * A "relative" URL does not include a scheme and must include a path. The
219 * authority, query, and fragment components are optional.
221 * This function splits the $url argument into the following components
222 * and returns them in an associative array. Keys to that array include:
224 * "scheme" The scheme, such as "http".
225 * "host" The host name, IPv4, or IPv6 address.
226 * "port" The port number.
227 * "user" The user name.
228 * "pass" The user password.
229 * "path" The path, such as a file path for "http".
231 * "fragment" The fragment.
233 * One or more of these may not be present, depending upon the URL.
235 * Optionally, the "user", "pass", "host" (if a name, not an IP address),
236 * "path", "query", and "fragment" may have percent-encoded characters
237 * decoded. The "scheme" and "port" cannot include percent-encoded
238 * characters and are never decoded. Decoding occurs after the URL has
242 * url the URL to parse.
244 * decode an optional boolean flag selecting whether
245 * to decode percent encoding or not. Default = TRUE.
248 * the associative array of URL parts, or FALSE if the URL is
249 * too malformed to recognize any parts.
251 function split_url( $url, $decode=TRUE )
253 // Character sets from RFC3986.
254 $xunressub = 'a-zA-Z\d\-._~\!$&\'()*+,;=';
255 $xpchar = $xunressub . ':@%';
257 // Scheme from RFC3986.
258 $xscheme = '([a-zA-Z][a-zA-Z\d+-.]*)';
260 // User info (user + password) from RFC3986.
261 $xuserinfo = '(([' . $xunressub . '%]*)' .
262 '(:([' . $xunressub . ':%]*))?)';
264 // IPv4 from RFC3986 (without digit constraints).
265 $xipv4 = '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})';
267 // IPv6 from RFC2732 (without digit and grouping constraints).
268 $xipv6 = '(\[([a-fA-F\d.:]+)\])';
270 // Host name from RFC1035. Technically, must start with a letter.
271 // Relax that restriction to better parse URL structure, then
272 // leave host name validation to application.
273 $xhost_name = '([a-zA-Z\d-.%]+)';
275 // Authority from RFC3986. Skip IP future.
276 $xhost = '(' . $xhost_name . '|' . $xipv4 . '|' . $xipv6 . ')';
278 $xauthority = '((' . $xuserinfo . '@)?' . $xhost .
279 '?(:' . $xport . ')?)';
281 // Path from RFC3986. Blend absolute & relative for efficiency.
282 $xslash_seg = '(/[' . $xpchar . ']*)';
283 $xpath_authabs = '((//' . $xauthority . ')((/[' . $xpchar . ']*)*))';
284 $xpath_rel = '([' . $xpchar . ']+' . $xslash_seg . '*)';
285 $xpath_abs = '(/(' . $xpath_rel . ')?)';
286 $xapath = '(' . $xpath_authabs . '|' . $xpath_abs .
287 '|' . $xpath_rel . ')';
289 // Query and fragment from RFC3986.
290 $xqueryfrag = '([' . $xpchar . '/?' . ']*)';
293 $xurl = '^(' . $xscheme . ':)?' . $xapath . '?' .
294 '(\?' . $xqueryfrag . ')?(#' . $xqueryfrag . ')?$';
297 // Split the URL into components.
298 if ( !preg_match( '!' . $xurl . '!', $url, $m ) )
301 if ( !empty($m[2]) ) $parts['scheme'] = strtolower($m[2]);
303 if ( !empty($m[7]) ) {
304 if ( isset( $m[9] ) ) $parts['user'] = $m[9];
305 else $parts['user'] = '';
307 if ( !empty($m[10]) ) $parts['pass'] = $m[11];
309 if ( !empty($m[13]) ) $h=$parts['host'] = $m[13];
310 else if ( !empty($m[14]) ) $parts['host'] = $m[14];
311 else if ( !empty($m[16]) ) $parts['host'] = $m[16];
312 else if ( !empty( $m[5] ) ) $parts['host'] = '';
313 if ( !empty($m[17]) ) $parts['port'] = $m[18];
315 if ( !empty($m[19]) ) $parts['path'] = $m[19];
316 else if ( !empty($m[21]) ) $parts['path'] = $m[21];
317 else if ( !empty($m[25]) ) $parts['path'] = $m[25];
319 if ( !empty($m[27]) ) $parts['query'] = $m[28];
320 if ( !empty($m[29]) ) $parts['fragment']= $m[30];
324 if ( !empty($parts['user']) )
325 $parts['user'] = rawurldecode( $parts['user'] );
326 if ( !empty($parts['pass']) )
327 $parts['pass'] = rawurldecode( $parts['pass'] );
328 if ( !empty($parts['path']) )
329 $parts['path'] = rawurldecode( $parts['path'] );
331 $parts['host'] = rawurldecode( $parts['host'] );
332 if ( !empty($parts['query']) )
333 $parts['query'] = rawurldecode( $parts['query'] );
334 if ( !empty($parts['fragment']) )
335 $parts['fragment'] = rawurldecode( $parts['fragment'] );
340 * This function joins together URL components to form a complete URL.
342 * RFC3986 specifies the components of a Uniform Resource Identifier (URI).
343 * This function implements the specification's "component recomposition"
344 * algorithm for combining URI components into a full URI string.
346 * The $parts argument is an associative array containing zero or
347 * more of the following:
349 * "scheme" The scheme, such as "http".
350 * "host" The host name, IPv4, or IPv6 address.
351 * "port" The port number.
352 * "user" The user name.
353 * "pass" The user password.
354 * "path" The path, such as a file path for "http".
356 * "fragment" The fragment.
358 * The "port", "user", and "pass" values are only used when a "host"
361 * The optional $encode argument indicates if appropriate URL components
362 * should be percent-encoded as they are assembled into the URL. Encoding
363 * is only applied to the "user", "pass", "host" (if a host name, not an
364 * IP address), "path", "query", and "fragment" components. The "scheme"
365 * and "port" are never encoded. When a "scheme" and "host" are both
366 * present, the "path" is presumed to be hierarchical and encoding
367 * processes each segment of the hierarchy separately (i.e., the slashes
370 * The assembled URL string is returned.
373 * parts an associative array of strings containing the
374 * individual parts of a URL.
376 * encode an optional boolean flag selecting whether
377 * to do percent encoding or not. Default = true.
380 * Returns the assembled URL string. The string is an absolute
381 * URL if a scheme is supplied, and a relative URL if not. An
382 * empty string is returned if the $parts array does not contain
383 * any of the needed values.
385 function join_url( $parts, $encode=TRUE )
389 if ( isset( $parts['user'] ) )
390 $parts['user'] = rawurlencode( $parts['user'] );
391 if ( isset( $parts['pass'] ) )
392 $parts['pass'] = rawurlencode( $parts['pass'] );
393 if ( isset( $parts['host'] ) &&
394 !preg_match( '!^(\[[\da-f.:]+\]])|([\da-f.:]+)$!ui', $parts['host'] ) )
395 $parts['host'] = rawurlencode( $parts['host'] );
396 if ( !empty( $parts['path'] ) )
397 $parts['path'] = preg_replace( '!%2F!ui', '/',
398 rawurlencode( $parts['path'] ) );
399 if ( isset( $parts['query'] ) )
400 $parts['query'] = rawurlencode( $parts['query'] );
401 if ( isset( $parts['fragment'] ) )
402 $parts['fragment'] = rawurlencode( $parts['fragment'] );
406 if ( !empty( $parts['scheme'] ) )
407 $url .= $parts['scheme'] . ':';
408 if ( isset( $parts['host'] ) )
411 if ( isset( $parts['user'] ) )
413 $url .= $parts['user'];
414 if ( isset( $parts['pass'] ) )
415 $url .= ':' . $parts['pass'];
418 if ( preg_match( '!^[\da-f]*:[\da-f.:]+$!ui', $parts['host'] ) )
419 $url .= '[' . $parts['host'] . ']'; // IPv6
421 $url .= $parts['host']; // IPv4 or name
422 if ( isset( $parts['port'] ) )
423 $url .= ':' . $parts['port'];
424 if ( !empty( $parts['path'] ) && $parts['path'][0] != '/' )
427 if ( !empty( $parts['path'] ) )
428 $url .= $parts['path'];
429 if ( isset( $parts['query'] ) )
430 $url .= '?' . $parts['query'];
431 if ( isset( $parts['fragment'] ) )
432 $url .= '#' . $parts['fragment'];
436 * Extract URLs from a web page.
438 * URLs are extracted from a long list of tags and attributes as defined
439 * by the HTML 2.0, HTML 3.2, HTML 4.01, and draft HTML 5.0 specifications.
440 * URLs are also extracted from tags and attributes that are common
441 * extensions of HTML, from the draft Forms 2.0 specification, from XHTML,
442 * and from WML 1.3 and 2.0.
444 * The function returns an associative array of associative arrays of
445 * arrays of URLs. The outermost array's keys are the tag (element) name,
446 * such as "a" for <a> or "img" for <img>. The values for these entries
447 * are associative arrays where the keys are attribute names for those
448 * tags, such as "href" for <a href="...">. Finally, the values for
449 * those arrays are URLs found in those tags and attributes throughout
453 * text the UTF-8 text to scan
456 * an associative array where keys are tags and values are an
457 * associative array where keys are attributes and values are
461 * http://nadeausoftware.com/articles/2008/01/php_tip_how_extract_urls_web_page
463 function extract_html_urls( $text )
465 $match_elements = array(
467 array('element'=>'a', 'attribute'=>'href'), // 2.0
468 array('element'=>'a', 'attribute'=>'urn'), // 2.0
469 array('element'=>'base', 'attribute'=>'href'), // 2.0
470 array('element'=>'form', 'attribute'=>'action'), // 2.0
471 array('element'=>'img', 'attribute'=>'src'), // 2.0
472 array('element'=>'link', 'attribute'=>'href'), // 2.0
474 array('element'=>'applet', 'attribute'=>'code'), // 3.2
475 array('element'=>'applet', 'attribute'=>'codebase'), // 3.2
476 array('element'=>'area', 'attribute'=>'href'), // 3.2
477 array('element'=>'body', 'attribute'=>'background'), // 3.2
478 array('element'=>'img', 'attribute'=>'usemap'), // 3.2
479 array('element'=>'input', 'attribute'=>'src'), // 3.2
481 array('element'=>'applet', 'attribute'=>'archive'), // 4.01
482 array('element'=>'applet', 'attribute'=>'object'), // 4.01
483 array('element'=>'blockquote', 'attribute'=>'cite'), // 4.01
484 array('element'=>'del', 'attribute'=>'cite'), // 4.01
485 array('element'=>'frame', 'attribute'=>'longdesc'), // 4.01
486 array('element'=>'frame', 'attribute'=>'src'), // 4.01
487 array('element'=>'head', 'attribute'=>'profile'), // 4.01
488 array('element'=>'iframe', 'attribute'=>'longdesc'), // 4.01
489 array('element'=>'iframe', 'attribute'=>'src'), // 4.01
490 array('element'=>'img', 'attribute'=>'longdesc'), // 4.01
491 array('element'=>'input', 'attribute'=>'usemap'), // 4.01
492 array('element'=>'ins', 'attribute'=>'cite'), // 4.01
493 array('element'=>'object', 'attribute'=>'archive'), // 4.01
494 array('element'=>'object', 'attribute'=>'classid'), // 4.01
495 array('element'=>'object', 'attribute'=>'codebase'), // 4.01
496 array('element'=>'object', 'attribute'=>'data'), // 4.01
497 array('element'=>'object', 'attribute'=>'usemap'), // 4.01
498 array('element'=>'q', 'attribute'=>'cite'), // 4.01
499 array('element'=>'script', 'attribute'=>'src'), // 4.01
501 array('element'=>'audio', 'attribute'=>'src'), // 5.0
502 array('element'=>'command', 'attribute'=>'icon'), // 5.0
503 array('element'=>'embed', 'attribute'=>'src'), // 5.0
504 array('element'=>'event-source','attribute'=>'src'), // 5.0
505 array('element'=>'html', 'attribute'=>'manifest'), // 5.0
506 array('element'=>'source', 'attribute'=>'src'), // 5.0
507 array('element'=>'video', 'attribute'=>'src'), // 5.0
508 array('element'=>'video', 'attribute'=>'poster'), // 5.0
510 array('element'=>'bgsound', 'attribute'=>'src'), // Extension
511 array('element'=>'body', 'attribute'=>'credits'), // Extension
512 array('element'=>'body', 'attribute'=>'instructions'), // Extension
513 array('element'=>'body', 'attribute'=>'logo'), // Extension
514 array('element'=>'div', 'attribute'=>'href'), // Extension
515 array('element'=>'div', 'attribute'=>'src'), // Extension
516 array('element'=>'embed', 'attribute'=>'code'), // Extension
517 array('element'=>'embed', 'attribute'=>'pluginspage'), // Extension
518 array('element'=>'html', 'attribute'=>'background'), // Extension
519 array('element'=>'ilayer', 'attribute'=>'src'), // Extension
520 array('element'=>'img', 'attribute'=>'dynsrc'), // Extension
521 array('element'=>'img', 'attribute'=>'lowsrc'), // Extension
522 array('element'=>'input', 'attribute'=>'dynsrc'), // Extension
523 array('element'=>'input', 'attribute'=>'lowsrc'), // Extension
524 array('element'=>'table', 'attribute'=>'background'), // Extension
525 array('element'=>'td', 'attribute'=>'background'), // Extension
526 array('element'=>'th', 'attribute'=>'background'), // Extension
527 array('element'=>'layer', 'attribute'=>'src'), // Extension
528 array('element'=>'xml', 'attribute'=>'src'), // Extension
530 array('element'=>'button', 'attribute'=>'action'), // Forms 2.0
531 array('element'=>'datalist', 'attribute'=>'data'), // Forms 2.0
532 array('element'=>'form', 'attribute'=>'data'), // Forms 2.0
533 array('element'=>'input', 'attribute'=>'action'), // Forms 2.0
534 array('element'=>'select', 'attribute'=>'data'), // Forms 2.0
537 array('element'=>'html', 'attribute'=>'xmlns'),
540 array('element'=>'access', 'attribute'=>'path'), // 1.3
541 array('element'=>'card', 'attribute'=>'onenterforward'), // 1.3
542 array('element'=>'card', 'attribute'=>'onenterbackward'),// 1.3
543 array('element'=>'card', 'attribute'=>'ontimer'), // 1.3
544 array('element'=>'go', 'attribute'=>'href'), // 1.3
545 array('element'=>'option', 'attribute'=>'onpick'), // 1.3
546 array('element'=>'template', 'attribute'=>'onenterforward'), // 1.3
547 array('element'=>'template', 'attribute'=>'onenterbackward'),// 1.3
548 array('element'=>'template', 'attribute'=>'ontimer'), // 1.3
549 array('element'=>'wml', 'attribute'=>'xmlns'), // 2.0
552 $match_metas = array(
560 // Extract all elements
561 if ( !preg_match_all( '/<([a-z][^>]*)>/iu', $text, $matches ) )
563 $elements = $matches[1];
564 $value_pattern = '=(("([^"]*)")|([^\s]*))';
566 // Match elements and attributes
567 foreach ( $match_elements as $match_element )
569 $name = $match_element['element'];
570 $attr = $match_element['attribute'];
571 $pattern = '/^' . $name . '\s.*' . $attr . $value_pattern . '/iu';
572 if ( $name == 'object' )
573 $split_pattern = '/\s*/u'; // Space-separated URL list
574 else if ( $name == 'archive' )
575 $split_pattern = '/,\s*/u'; // Comma-separated URL list
577 unset( $split_pattern ); // Single URL
578 foreach ( $elements as $element )
580 if ( !preg_match( $pattern, $element, $match ) )
582 $m = empty($match[3]) ?
(!empty($match[4])?
$match[4]:'') : $match[3];
583 if ( !isset( $split_pattern ) )
584 $urls[$name][$attr][] = $m;
587 $msplit = preg_split( $split_pattern, $m );
588 foreach ( $msplit as $ms )
589 $urls[$name][$attr][] = $ms;
594 // Match meta http-equiv elements
595 foreach ( $match_metas as $match_meta )
597 $attr_pattern = '/http-equiv="?' . $match_meta . '"?/iu';
598 $content_pattern = '/content' . $value_pattern . '/iu';
599 $refresh_pattern = '/\d*;\s*(url=)?(.*)$/iu';
600 foreach ( $elements as $element )
602 if ( !preg_match( '/^meta/iu', $element ) ||
603 !preg_match( $attr_pattern, $element ) ||
604 !preg_match( $content_pattern, $element, $match ) )
606 $m = empty($match[3]) ?
$match[4] : $match[3];
607 if ( $match_meta != 'refresh' )
608 $urls['meta']['http-equiv'][] = $m;
609 else if ( preg_match( $refresh_pattern, $m, $match ) )
610 $urls['meta']['http-equiv'][] = $match[2];
614 // Match style attributes
615 $urls['style'] = array( );
616 $style_pattern = '/style' . $value_pattern . '/iu';
617 foreach ( $elements as $element )
619 if ( !preg_match( $style_pattern, $element, $match ) )
621 $m = empty($match[3]) ?
$match[4] : $match[3];
622 $style_urls = extract_css_urls( $m );
623 if ( !empty( $style_urls ) )
624 $urls['style'] = array_merge_recursive(
625 $urls['style'], $style_urls );
628 // Match style bodies
629 if ( preg_match_all( '/<style[^>]*>(.*?)<\/style>/siu', $text, $style_bodies ) )
631 foreach ( $style_bodies[1] as $style_body )
633 $style_urls = extract_css_urls( $style_body );
634 if ( !empty( $style_urls ) )
635 $urls['style'] = array_merge_recursive(
636 $urls['style'], $style_urls );
639 if ( empty($urls['style']) )
640 unset( $urls['style'] );
645 * Extract URLs from UTF-8 CSS text.
647 * URLs within @import statements and url() property functions are extracted
648 * and returned in an associative array of arrays. Array keys indicate
649 * the use context for the URL, including:
654 * Each value in the associative array is an array of URLs.
657 * text the UTF-8 text to scan
660 * an associative array of arrays of URLs.
663 * http://nadeausoftware.com/articles/2008/01/php_tip_how_extract_urls_css_file
665 function extract_css_urls( $text )
669 $url_pattern = '(([^\\\\\'", \(\)]*(\\\\.)?)+)';
670 $urlfunc_pattern = 'url\(\s*[\'"]?' . $url_pattern . '[\'"]?\s*\)';
672 '(@import\s*[\'"]' . $url_pattern . '[\'"])' .
673 '|(@import\s*' . $urlfunc_pattern . ')' .
674 '|(' . $urlfunc_pattern . ')' . ')/iu';
675 if ( !preg_match_all( $pattern, $text, $matches ) )
680 foreach ( $matches[3] as $match )
681 if ( !empty($match) )
683 preg_replace( '/\\\\(.)/u', '\\1', $match );
686 // @import url('...')
687 // @import url("...")
688 foreach ( $matches[7] as $match )
689 if ( !empty($match) )
691 preg_replace( '/\\\\(.)/u', '\\1', $match );
696 foreach ( $matches[11] as $match )
697 if ( !empty($match) )
698 $urls['property'][] =
699 preg_replace( '/\\\\(.)/u', '\\1', $match );