H
H
hyget2018-03-19 12:11:43
PHP
hyget, 2018-03-19 12:11:43

How to get a link regularly?

Hello, you need to select all url links from:
a href=" https://site.com/aaa-bbb-ccc-ddd-eee-fff-eee "
a href=" https://site.com/aaaaa- bbbb-ccccccc-ddddd-eeeee "
but under one condition: if the link contains more than 3 characters" - "
i.e. such a link a href=" https://site.com/aaa-bbb-ccc will not work
Tell me please how can this be done

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolay, 2018-03-19
@hyget

  1. Regular to catch all linkshref\=\"(.*)\"
  2. Well, the number of "dashes" is better to find out by breaking the string with explode and looking at the "length" of the array
$output = [];
$html = '
<a href="https://site.com/aaa-bbb-ccc-ddd-eee-fff-eee">
<a href="https://site.com/aaa-bbb-ccc">
<a href="https://site.com/aaaaa-bbbb-ccccccc-ddddd-eeeee">';

preg_match_all('/href\=\"(.*)\"/', $html, $matches);

foreach($matches[1] as $link) {
    $dashes = explode('-', $link);
    if (count($dashes) > 3) $output[] = $link;
}

var_dump($output);

O
Orkhan Hasanli, 2018-03-19
@azerphoenix

Hello!
There is a site for generating regular expressions - txt2re.com will
suddenly help you)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question