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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.