如果href或src的开头不是http,https或www,则PHP删除特定的标记或img标记 - php

如果aimg标记内的$string_1<src>不是以www,http或https开头,我想从<href>删除特定的<a><img>标记。

例如,通过删除以下内容,将$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 &gt;</a>

因为srchref标记不是以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 &gt;</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 &gt;</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 &gt;</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>
';

您能帮我解决这个问题吗?谢谢

参考方案

这是PHP中的第一种方法。它适用于您的示例数据。 $ string_2中缺少尾部的“

”。

$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);

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

提交表单后显示模式对话框 - php

提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…

使用Ajax呈现html表 - php

我想知道如何实现以下项目其实我有一个php代码,可以渲染一张桌子<table id = "oldata" class ="table table-bordered"> <thead> <tr class ="success"> <th class="…

PHP stringID无法从回显中读入onclick - javascript

我有一个音频播放器,可以在HTML中正常工作,但是当我从PHP回显调用时,似乎没有得到div ID的名称,因此它无法播放我的音频。这是我的代码:// Show audio if ($sObj->get('audio') != null) { $sAudio = $sObj->get('audio'); $a…

使用XPath在PHP中提取XML - php

我有以下XML:<root> <level name="level1"> <!-- More children <level> --> </level> <level name="level2"> <!-- Some more childre…