PHP-Удаление определенного тега a или тега img, если href или src не начинаются с http, https или www

#php

#php

Вопрос:

Я хочу удалить определенные a img теги and из $string_1 if <src> или <href> не начинаются с www, http или https внутри <a> <img> тегов or.

Например, $string_1 преобразуется в $string_2 путем удаления:

 <img src="/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/>
 

и

 <a href="/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth amp;></a>
 

потому src href что теги and не начинаются с http, https или www.

 $string_1 = '
<div class="mainpost"><p><img src="/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/></p>
<div class="mainpost"><p><img src="http://www.domain.com/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/></p>
<p><a href="http://domain.com/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth amp;></a></p>
<p>Photography Business Growth | With a world wide recession, photographers and small business owners are forced, more than ever, to think creatively, to think differently and outside of the box. With very little or no money to invest in your business, can you move forward? How can you build your brand and make sure to get happier, paying clients through your door?<br/><span id="more-609494"/></p>
<p>If you take good shots it doesn’t mean you’ll gain success and popularity among customers. For those of you who have survived start=up and built successful brands, you may be wondering which step to take next to grow your business beyond its current status. There are numerous possibilities, some of which we’ll outline here. You need to know how to sell yourself well! Everything is quite simple and you can do it yourself.</p>
<p><a href="/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth amp;></a></p>
';

$string_2= '
<div class="mainpost"><p></p>
<div class="mainpost"><p><img src="http://www.domain.com/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/></p>
<p><a href="http://domain.com/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth amp;></a></p>
<p>Photography Business Growth | With a world wide recession, photographers and small business owners are forced, more than ever, to think creatively, to think differently and outside of the box. With very little or no money to invest in your business, can you move forward? How can you build your brand and make sure to get happier, paying clients through your door?<br/><span id="more-609494"/></p>
<p>If you take good shots it doesn’t mean you’ll gain success and popularity among customers. For those of you who have survived start=up and built successful brands, you may be wondering which step to take next to grow your business beyond its current status. There are numerous possibilities, some of which we’ll outline here. You need to know how to sell yourself well! Everything is quite simple and you can do it yourself.</p>
';
 

Не могли бы вы помочь мне решить эту проблему? Спасибо

Комментарии:

1. честно говоря, это то, что лучше передается с помощью javascript / jquery. Он может анализировать и манипулировать строками html лучше, чем когда-либо мог PHP.

2. Если вы делаете это в WordPress, есть более элегантные решения.. Редактировать: нужна дополнительная информация, которую этот вопрос не предоставляет.

Ответ №1:

Вот первый подход в PHP. Это работает для вашего примера данных. В $string_2 отсутствовал хвост «<p></p>».

 $string_3 = $string_1;
$pattern = "([^wh]|w[^w]|ww[^w]|h[^t]|ht[^t]|htt[^p])";
$string_3 = preg_replace("/<img src="".$pattern."[^>]*>/","",$string_3);
$string_3 = preg_replace("/<a href="".$pattern."[^>]*>[^<]*</a>/","",$string_3);
 

Комментарии:

1. спасибо за ваш ответ, ваш ответ правильный, и я проверил здесь codepad.viper-7.com/bPMlpB . однако, когда я получаю данные из таблицы MySQL $string_3 = mysqli_real_escape_string($db_connect, $row['string_3']); , он не применяет фильтр перед вставкой в другую таблицу, есть идеи?

2. mysqli_real_escape_string() — это команда, которая экранирует специальные символы, чтобы использовать их в следующей инструкции SQL: кодируются символы NUL (ASCII 0), n, r, , ‘, » и Control-Z -> Поэтому сопоставление кавычек завершится ошибкой. Вам просто нужно выполнить замену перед mysqli_real_escape_string .

3. Я нашел проблему, еще раз спасибо за ваш код, лучший

Ответ №2:

Я бы использовал DOM для этого синтаксический анализатор. Имея документ DOM, вы можете использовать XPath его для выбора нужных элементов.

 # Parse the HTML snippet into a DOM document
$doc = new DOMDocument();
$doc->loadHTML($string_1);

# Create an XPath selector
$selector = new DOMXPath($doc);

# Define the XPath query
# The syntax highlighter messed this up. Take it as it is!
$query = <<<EOF
  //a[not(starts-with(@href, "http"))
  and not(starts-with(@href, "www"))]
| //img[not(starts-with(@src, "http"))
  and not(starts-with(@src, "www"))]
EOF;

# Issue the XPath query and remove every resulting node
foreach($selector->query($query) as $node) {
    $node->parentNode->removeChild($node);
}

# Write back the modified `<div>` element into a string
echo $doc->saveHTML(
    $selector->query('//div[@class="mainpost"]')->item(0)
);
 

Ответ №3:

Одним из решений было бы сделать это на интерфейсе с помощью Javascript. Если это невозможно, вы можете обратиться к библиотеке PHP для анализа и обхода DOM, например http://simplehtmldom.sourceforge.net