A
A
Alex Wells2016-02-09 23:03:57
PHP
Alex Wells, 2016-02-09 23:03:57

How to filter all words from the list, so that it does not equal another?

Hello. The task is to filter unwanted links on the site. Everything would be fine, but at the same time you need to skip a specific link / links from the filter. For example, the line looks like this: "some words link.com mysite.ru somelink.net"
In this case, link.com and somelink.net should be replaced with something, but mysite.ru should be left. So far, I've dug out this RegExp:

/(http(s)?:\/\/)?(www\.)?(те|самые|нежелательные|слова|которые|находятся|в|домене)[\w\d-.а-аЏаА-б]{0,15}(\.com|\.ru|\.net|\.gl|.бб|\.pro|\.tv)(\/)?/i

In theory, if these words are in the link - RegExp passes. But you need to take into account that if the entire "object" that php scans is equal to "mysite.ru" - it does not work. I hope the essence is clear, thanks in advance for the answers!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2016-02-11
@Alex_Wells

<?php

$input = 'some words link.com mysite.ru somelink.net';
$forbidden = ['link.com', 'somelink.net'];

// Экранируем все спецсимволы в списке запрещённых сайтов
$regexp = implode(array_map('preg_quote', $forbidden), '|');
$output = preg_replace('/(https?:\/\/)?(www\.)?(' . $regexp . ')/i', '***', $input);

https://ideone.com/FMyBPF
It is possible and simpler:
<?php

$input = 'some words link.com mysite.ru somelink.net';
$output = preg_replace('/(https?:\/\/)?(www\.)?(link\.com|somelink\.net)/i', '***', $input);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question