Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
<?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);
<?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 questionAsk a Question
731 491 924 answers to any question