phuochq
phuochq

Reputation: 37

PHP compare multiple string

I have a lot of links:

http://exmpale.com/abc/exmpale.html
http://exmpale.com/abc/exmpale1.html
http://exmpale.com/abc/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale1.html
http://exmpale.com/abc/exmpale/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale3.html
http://exmpale.com/abcd/exmpale1.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale4.html
http://exmpale.com/abc/abc/exmpale1.html
http://exmpale.com/abc/abc/exmpale2.html
http://exmpale.com/abc/abc/exmpale3.html
http://exmpale.com/abc/abc/exmpale4.html
http://exmpale.com/abc/abc/exmpale5.html

I use php, i want to get all links like http://exmpale.com/abc/(*).html and list it in array. I'm sorry, I forgot code. code of me

$a = array(
    0 => 'http://exmpale.com/abc/exmpale.html',
    1 => 'http://exmpale.com/abc/exmpale1.html',
    2 => 'http://exmpale.com/abc/exmpale2.html',
    3 => 'http://exmpale.com/abc/exmpale/exmpale1.html',
    4 => 'http://exmpale.com/abc/exmpale/exmpale2.html',
    5 => 'http://exmpale.com/abc/exmpale/exmpale3.html',
    6 => 'http://exmpale.com/abcd/exmpale1.html',
    7 => 'http://exmpale.com/abcd/exmpale2.html',
    8 => 'http://exmpale.com/abcd/exmpale2.html',
    9 => 'http://exmpale.com/abcd/exmpale4.html',
    10 => 'http://exmpale.com/abc/abc/exmpale1.html',
    11 => 'http://exmpale.com/abc/abc/exmpale2.html',
    12 => 'http://exmpale.com/abc/abc/exmpale3.html',
    13 => 'http://exmpale.com/abc/abc/exmpale4.html',
    14 => 'http://exmpale.com/abc/abc/exmpale5.html',
);

foreach($a as $k => $v ){
    $abc = preg_match_all('/http\:\/\/exmpale\.com\/abc\/(.*?)\.html/',$v,$b);  
    print_r($b[1]);
}

and I want to get the result.

$result = array(
  'http://exmpale.com/abc/exmpale.html',
  'http://exmpale.com/abc/exmpale1.html',
  'http://exmpale.com/abc/exmpale2.html');

but it took all result, please help me thanks you.

Upvotes: 0

Views: 94

Answers (2)

msfoster
msfoster

Reputation: 2572

Since you dont provide what kind of data structure the urls are stored in, I assume it's a raw string:

$data_raw = "http://exmpale.com/abc/exmpale.html
http://exmpale.com/abc/exmpale1.html
http://exmpale.com/abc/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale1.html
http://exmpale.com/abc/exmpale/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale3.html
http://exmpale.com/abcd/exmpale1.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale4.html
http://exmpale.com/abc/abc/exmpale1.html
http://exmpale.com/abc/abc/exmpale2.html
http://exmpale.com/abc/abc/exmpale3.html
http://exmpale.com/abc/abc/exmpale4.html
http://exmpale.com/abc/abc/exmpale5.html";

$urls = explode("\n", $data_raw);

$accept_host = "exmpale.com";
$accept_path = "/abc/";

$filtered_urls = array_filter($urls, function($url) use($accept_host, $accept_path) {
    $parts = parse_url($url);   
    return $parts['host'] == $accept_host
        && strpos($parts['path'], $accept_path) === 0
        && strpos($parts['path'], '/', strlen($accept_path)) === false;
});

printf("<pre>%s</pre>", print_r($filtered_urls, true));

Yields:

Array
(
    [0] => http://exmpale.com/abc/exmpale.html
    [1] => http://exmpale.com/abc/exmpale1.html
    [2] => http://exmpale.com/abc/exmpale2.html
)

Upvotes: 0

Nikster2014
Nikster2014

Reputation: 409

Simply use the php function preg_match_all and the matched string will get loaded in an array that you supply to the above function as an argument. Please read the PHP manual on preg_match_all and create a regex for your needs. You can test your regex here

Upvotes: 1

Related Questions