Jump to content

preg_replace help


tobeyt23

Recommended Posts

I am getting a rss feed of ads and I need to remove some on the string so that i can use it properly.

$ad = urldecode("<div class="image-advertisement" id="ad-306"><a href="http://www.appraisalbuzz.com/ad/redirect/306/0" onclick="window.open(this.href); return false;"><img src="http://appraisalbuzz.com/sites/default/files/FREA_BannerWebinarBrianT.gif" alt="" title="" width="468" height="60" /></a></div><div class="links-to">Links to https://www.frea.com/.</div>");
echo preg_replace('/<div class="image-advertisement" id="ad-{0-9}+">/', '', $ad);

 

What I am trying to do is remove :

<div class="image-advertisement" id="ad-306"> and then </div><div class="links-to">Links to https://www.frea.com/.</div>. The problem is the id="ad-XXX" will always be different as well as the </div><div class="links-to">Links to  XXXXXXXXXXXXXXXXXXX</div>

 

The end result should be :

<a href="http://www.appraisalbuzz.com/ad/redirect/306/0" onclick="window.open(this.href); return false;"><img src="http://appraisalbuzz.com/sites/default/files/FREA_BannerWebinarBrianT.gif" alt="" title="" width="468" height="60" /></a>

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/265864-preg_replace-help/
Share on other sites

I have made some progress just can't seem to get the url :

 

<?php
$ad = "<div class="image-advertisement" id="ad-306"><a href="http://www.appraisalbuzz.com/ad/redirect/306/0" onclick="window.open(this.href); return false;"><img src="http://appraisalbuzz.com/sites/default/files/FREA_BannerWebinarBrianT.gif" alt="" title="" width="468" height="60" /></a></div><div class="links-to">Links to https://www.frea.com/.</div>";
$pattern = array();
$pattern[0] = '/<div class="image-advertisement" id="ad-[0-9]+">/';
$pattern[1] = '/<\/div><div class="links-to">Links to ((?:http:\/\/)?[a-zA-Z0-9:\._-]+)[\'\"]>(.*?)<\/div> /';
$replace = array();
$replace[0] = '';
$replace[1] = '';
echo preg_replace($pattern, $replace, $ad);
?>

Link to comment
https://forums.phpfreaks.com/topic/265864-preg_replace-help/#findComment-1362320
Share on other sites

Think i got it:

<?php
$ad = "<div class="image-advertisement" id="ad-306"><a href="http://www.appraisalbuzz.com/ad/redirect/306/0" onclick="window.open(this.href); return false;"><img src="http://appraisalbuzz.com/sites/default/files/FREA_BannerWebinarBrianT.gif" alt="" title="" width="468" height="60" /></a></div><div class="links-to">Links to http://www.frea.com/</div>";
$pattern = array();
$pattern[0] = '/<div class="image-advertisement" id="ad-[0-9]+">/';
$pattern[1] = '/<\/div><div class="links-to">Links to (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?+/';
$replace = array();
$replace[0] = '';
$replace[1] = '';
echo preg_replace($pattern, $replace, $ad);
?>

Link to comment
https://forums.phpfreaks.com/topic/265864-preg_replace-help/#findComment-1362321
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.