Jump to content

help - replacing adsense pid with


prakash

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/50690-help-replacing-adsense-pid-with/
Share on other sites

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

<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>

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.