0

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.

2
  • I suggest you take a look at loops and substring functions. Start looking into the documentation, start using google for that. Then start your own script, try to solve this yourself. Then, if you run into a specific issue with your own code, then is the time to ask here and post your code inside the question. We are not here to do your work for you. We are here to help you with getting your work done. Commented Oct 14, 2016 at 8:18
  • sorry, i frogot code of me Commented Oct 14, 2016 at 9:04

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

i use function preg_match_all but result not like
0

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
)

5 Comments

thanks you. but data in string and I want to get result exmpale.com/abc/exmpale.html,http://exmpale.com/abc/… and exmpale.com/abc/exmpale2.html
I understand that you want each url seperated by a comma. Have a look at the update.
I just saw your update. The result you are asking for is the array $filtered_urls. Have you tried the code?
and in example it is three link me comment.
@phuochq Ok, so you will not accept any additional path parameters after /abc/?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.