prakash Posted May 9, 2007 Share Posted May 9, 2007 I need an reg expression code for replacing "google_ad_client" and "google_ad_channel" <script type="text/javascript"><!-- google_ad_client = "pub-0000000000000000"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "image"; google_ad_channel = "0000000000"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> thanks in advance Quote Link to comment Share on other sites More sharing options...
effigy Posted May 9, 2007 Share Posted May 9, 2007 <script type="text/javascript"><!-- var google_ad_client = "pub-0000000000000000"; var string = "abc" + google_ad_client + "def"; alert(string.replace(google_ad_client, '123')); //--> </script> This only works because google_ad_client does not have any metacharacters; if it did, you would get varying results. I'm not aware of any Javascript built-ins that properly quote a string being made into a pattern. Quote Link to comment Share on other sites More sharing options...
prakash Posted May 9, 2007 Author Share Posted May 9, 2007 actually the above code and some other stuffs is stored into the variable. can't we use "preg_replace" or "str_replace" using regex for that Quote Link to comment Share on other sites More sharing options...
effigy Posted May 9, 2007 Share Posted May 9, 2007 <pre> <?php $code = <<<CODE <script type="text/javascript"><!-- google_ad_client = "pub-0000000000000000"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "image"; google_ad_channel = "0000000000"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> CODE; $google_replace = array( 'client' => 'bob', 'channel' => '123', ); echo preg_replace( '/(google_ad_(c(?:lient|hannel))\s*=\s*")(.*?)(?=")/e', '"\1".$google_replace["\2"]', $code ); ?> </pre> Quote Link to comment 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.