tags:

views:

45

answers:

3

When i perform a regular expression

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

i got an array inside array. I need a single array only.

How to negotiate the arrays inside another arrays

A: 

Not sure what you mean by 'negociate'. If you mean fetch the inner array, that should work:

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

Generally you can replace your regexp with one that doesn't contain parenthesis (). This way your results will be hold just in the $turls[0] variable :

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

and then do some code to make urls unique like this:

$result = array_keys(array_flip($turls[0]));
budinov.com
A: 

By default preg_match_all() uses PREG_PATTERN_ORDER flag, which means:

Orders results so that $matches[0] is an array of full pattern matches, $matches1 is an array of strings matched by the first parenthesized subpattern, and so on.

See http://php.net/preg_match_all

Here is sample output:

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 => ...
    ),
    ...
)

So, as R. Hill answered, you need $matches[0] to access all matched urls. And as budinov.com pointed, you should remove outer parentheses to avoid second match duplicate first one, e.g.:

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