+ $_uri['area']['offset'] = $offset; // Area offset for area_measure()
+ }
+
+ return $array;
+}
+
+// Normalize an array of URI arrays
+// NOTE: Give me the uri_pickup() results
+function uri_pickup_normalize(& $pickups, $destructive = TRUE)
+{
+ if (! is_array($pickups)) return $pickups;
+
+ if ($destructive) {
+ foreach (array_keys($pickups) as $key) {
+ $_key = & $pickups[$key];
+ $_key['scheme'] = isset($_key['scheme']) ? scheme_normalize($_key['scheme']) : '';
+ $_key['host'] = isset($_key['host']) ? host_normalize($_key['host']) : '';
+ $_key['port'] = isset($_key['port']) ? port_normalize($_key['port'], $_key['scheme'], FALSE) : '';
+ $_key['path'] = isset($_key['path']) ? strtolower(path_normalize($_key['path'])) : '';
+ $_key['file'] = isset($_key['file']) ? file_normalize($_key['file']) : '';
+ $_key['query'] = isset($_key['query']) ? query_normalize($_key['query']) : '';
+ $_key['fragment'] = isset($_key['fragment']) ? strtolower($_key['fragment']) : '';
+ }
+ } else {
+ foreach (array_keys($pickups) as $key) {
+ $_key = & $pickups[$key];
+ $_key['scheme'] = isset($_key['scheme']) ? scheme_normalize($_key['scheme']) : '';
+ $_key['host'] = isset($_key['host']) ? strtolower($_key['host']) : '';
+ $_key['port'] = isset($_key['port']) ? port_normalize($_key['port'], $_key['scheme'], FALSE) : '';
+ $_key['path'] = isset($_key['path']) ? path_normalize($_key['path']) : '';
+ }
+ }
+
+ return $pickups;
+}
+
+// An URI array => An URI (See uri_pickup())
+// USAGE:
+// $pickups = uri_pickup('a string include some URIs');
+// $uris = array();
+// foreach (array_keys($pickups) as $key) {
+// $uris[$key] = uri_pickup_implode($pickups[$key]);
+// }
+function uri_pickup_implode($uri = array())
+{
+ if (empty($uri) || ! is_array($uri)) return NULL;
+
+ $tmp = array();
+ if (isset($uri['scheme']) && $uri['scheme'] !== '') {
+ $tmp[] = & $uri['scheme'];
+ $tmp[] = '://';
+ }
+ if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
+ $tmp[] = & $uri['userinfo'];
+ $tmp[] = '@';
+ }
+ if (isset($uri['host']) && $uri['host'] !== '') {
+ $tmp[] = & $uri['host'];
+ }
+ if (isset($uri['port']) && $uri['port'] !== '') {
+ $tmp[] = ':';
+ $tmp[] = & $uri['port'];
+ }
+ if (isset($uri['path']) && $uri['path'] !== '') {
+ $tmp[] = & $uri['path'];
+ }
+ if (isset($uri['file']) && $uri['file'] !== '') {
+ $tmp[] = & $uri['file'];
+ }
+ if (isset($uri['query']) && $uri['query'] !== '') {
+ $tmp[] = '?';
+ $tmp[] = & $uri['query'];
+ }
+ if (isset($uri['fragment']) && $uri['fragment'] !== '') {
+ $tmp[] = '#';
+ $tmp[] = & $uri['fragment'];
+ }
+
+ return implode('', $tmp);
+}
+
+// $array['something'] => $array['wanted']
+function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
+{
+ if (! is_array($array) || ! is_array($keys)) return FALSE;
+
+ // Nondestructive test
+ if (! $force)
+ foreach(array_keys($keys) as $from)
+ if (! isset($array[$from]))
+ return FALSE;
+
+ foreach($keys as $from => $to) {
+ if ($from === $to) continue;
+ if (! $force || isset($array[$from])) {
+ $array[$to] = & $array[$from];
+ unset($array[$from]);
+ } else {
+ $array[$to] = $default;
+ }
+ }
+
+ return TRUE;
+}
+
+// ---------------------
+// Area pickup
+
+// Pickup all of markup areas
+function area_pickup($string = '', $method = array())
+{
+ $area = array();
+ if (empty($method)) return $area;
+
+ // Anchor tag pair by preg_match and preg_match_all()
+ // [OK] <a href></a>
+ // [OK] <a href= >Good site!</a>
+ // [OK] <a href= "#" >test</a>
+ // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
+ // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
+ // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
+ $regex = '#<a\b[^>]*\bhref\b[^>]*>.*?</a\b[^>]*(>)#is';
+ if (isset($method['area_anchor'])) {
+ $areas = array();
+ $count = isset($method['asap']) ?
+ preg_match($regex, $string) :
+ preg_match_all($regex, $string, $areas);
+ if (! empty($count)) $area['area_anchor'] = $count;
+ }
+ if (isset($method['uri_anchor'])) {
+ $areas = array();
+ preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
+ foreach(array_keys($areas) as $_area) {
+ $areas[$_area] = array(
+ $areas[$_area][0][1], // Area start (<a href>)
+ $areas[$_area][1][1], // Area end (</a>)