I'm trying to do a preg_replace function for Google Ads for my website. I want it so that it will replace all of the AdSense code, but still be able to detect the ad size. I have the following: if (preg_match("/google_ad_width = 468; google_ad_height = 15;/", $input)) { $input = preg_replace("/<script type=\"text\/javascript\"><!-- (.*) google_ad_width = 468; (.*) google_ad_height = 15; (.*) \/\/--> <\/script>/", "blablabla", $input); } PHP: I know this is wrong, but here is what I am trying to do with it. Since most AdSense codes are always posted a little differently, I can't have it just one way because it will only detect that kind of format. I added a bunch of (.*) (catch all's I presume) just in case the adsense code has something above or below the ad height and width. But this isn't working. Can somebody help me out with the appropriate code. This is my very first time working with regex, and I am still very new to PHP overall.
$result = preg_replace('#<script type="text/javascript"><!--\s*(?:google_ad_(?:.+) = ".+";)*\s*google_ad_width = [0-9]+;\s*(?:google_ad_(?:.+) = ".+";)*\s*google_ad_height = [0-9]+;\s*(?:google_ad_(?:.+) = ".+";)*\s*//-->\s*</script>#si', '', $html); Code (PHP): Will match Im sure the regex can be optimized though.
Thanks However, would it match something like <script type="text/javascript"><!-- google_ad_client = "pub-4394305856464210"; google_ad_width = 728; google_ad_height = 15; google_ad_format = "728x15_0ads_al_s"; google_ad_channel = "4713084895"; google_color_border = "eff0f2"; google_color_bg = "ecedef"; google_color_link = "404e59"; google_color_text = "404e59"; google_color_url = "404e59"; //--> </script> HTML: or <script type="text/javascript"><!-- google_ad_client = "pub-6769684567192354"; /* Car Donation - Above Content Links 468x15 */ google_ad_slot = "3442718188"; google_ad_width = 468; google_ad_height = 15; //--> </script> HTML: or even something like this where the height and width aren't right next to each other? <script type="text/javascript"><!-- google_ad_client = "pub-5676236325709660"; google_ad_width = 468; google_ad_slot = "2170747119"; google_ad_height = 60; //--> </script> HTML: