A
A
Alex Ivanov2015-12-18 19:07:18
PHP
Alex Ivanov, 2015-12-18 19:07:18

How to do a universal link search using preg_match_all?

How to find all links that meet the following requirements using one query:
located on the domains mydomain1.com, site.ru, superdomen.org.ru ... the number of domains can be added. Also, the link can be

<a href="_сылка_">...</a>
<a href="_сылка_">...</a>
[url]_сылка_[/url]
[url=_сылка_]...[/URL]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
heartdevil, 2015-12-18
@heartdevil

Hello.
It can be like this.
Example
This part is repeated three times
To add another domain, simply use this entry.
We want to add google.com, then to this part mydomain1\.com.*|site.ru.*|superdomen\.org\.ru.* we add through the symbol | this entry: google.com.* And we get:
mydomain1\.com.*|site.ru.*|superdomen\.org\.ru.* |google.com.*
This is not the best option, if you think carefully, you can think of a better volume), but this is one of the easiest ways).

A
Alex Ivanov, 2015-12-18
@Protossan

Well, I was able to come up with something like this:

$string = "some text
  [url]http://dvz.local/index/index/regionId/28[/url]
        <a href=\"http://stuff.ru/roadmap_page.php\">wetrwetwert</a> [url=https://192.168.3.192/roadmap_page.php]qqqqqqqqq[/url]
         [URL]http://www.192.168.3.192/roadmap_page.php#qwe[/URL]";
preg_match_all('#(http|https)://([^\s]*)(192.168.3.192|stuff.ru)([^\s]*)#', $string, $matches);

var_dump($matches);

It is easier in terms of adding a new domain, but there is a problem with the ending:
array(3) {
    [0]=>
    string(49) "http://stuff.ru/roadmap_page.php">wetrwetwert</a>"
    [1]=>
    string(54) "https://192.168.3.192/roadmap_page.php]qqqqqqqqq[/url]"
    [2]=>
    string(51) "http://www.192.168.3.192/roadmap_page.php#qwe[/URL]"
  }

Those. the last segment of the link tag remains. How to remove it in the same regular season, so as not to cling to str_replace or preg_replace later. In fact, you need to make cuts for such characters - space, [, (, ", ' . But something doesn’t work out to be tied to the regular expression. How to do it more competently?
A solution that fully satisfies the task.
Thanks to everyone who responded and gave me ideas!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question