tobeyt23 Posted July 17, 2012 Share Posted July 17, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265864-preg_replace-help/ Share on other sites More sharing options...
tobeyt23 Posted July 18, 2012 Author Share Posted July 18, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/265864-preg_replace-help/#findComment-1362320 Share on other sites More sharing options...
tobeyt23 Posted July 18, 2012 Author Share Posted July 18, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/265864-preg_replace-help/#findComment-1362321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.