2010-07-20 57 views
0

内当我执行正则表达式协商阵列的阵列

preg_match_all('~(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)~', $content, $turls); 
print_r($turls); 

我得到内部阵列的阵列。我只需要一个数组。

如何协商另一个阵列

+0

什么是你'的print_r($ turls)'打印? – BoltClock 2010-07-20 17:19:11

+0

$ content其实是file_get_contents(“http://www.yahoo.com”);因此preg_match_all会从yahoo.com中提取链接,并将其存储在数组$ turls中 – Rajasekar 2010-07-20 17:22:40

回答

0

默认情况下preg_match_all()使用PREG_PATTERN_ORDER标志,这意味着:

结果排序使得$ matches [0]是 满图案匹配的数组, $匹配1是串0的数组由第一个加括号的 子模式匹配,依此类推。

http://php.net/preg_match_all

下面是示例输出:

array(
    0 => array(// Full pattern matches 
     0 => 'http://www.w3.org/TR/html4/strict.dtd', 
     1 => ... 
    ), 

    1 => array(// First parenthesized subpattern. 
       // In your case it is the same as full pattern, because first 
       // parenthesized subpattern includes all pattern :-) 
     0 => 'http://www.w3.org/TR/html4/strict.dtd', 
     1 => ... 
    ), 

    2 => array(// Second parenthesized subpattern. 
     0 => 'www.w3.org', 
     1 => ... 
    ), 
    ... 
) 

所以,作为R. Hill回答,你需要$比赛[0]来访问所有匹配的URL。 而作为budinov.com尖,你应该删除外括号来避免第二场比赛复制第一个,例如:

preg_match_all('~https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?~', $content, $turls); 
// where $turls[0] is what you need 
0

不知道你的“negociate”的意思内部的阵列。如果你的意思是获取内部阵列,应该工作:

$urls = preg_match_all('~(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)~', $content, $matches) ? $matches[0] : array(); 
if (count($urls)) { 
    ... 
    } 
0

一般来说,你可以用一个取代你的正则表达式不包含括号()。这样,你的结果将持有只是在$ turls [0]变量:

preg_match_all('/https?\:\/\/[^\"\'\s]+/i', file_get_contents('http://www.yahoo.com'), $turls); 

,然后做一些代码,使网址中的独立是这样的:

$result = array_keys(array_flip($turls[0]));