Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 649949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:59:59+00:00 2026-05-13T21:59:59+00:00

I have a txt file which contains a google adsense code block and I’m

  • 0

I have a txt file which contains a google adsense code block and I’m trying to pull in the file via file_get_contents to extract the values of the google_ad_client and google_ad_slot variables.

In the examples below, I want to return to my calling function:

$google_ad_client = 'pub-1234567890987654';
$google_ad_slot = '1234567890'

The file may contain one of either of these two formats and I wont know which the user has chosen:

Newer Ad Unit Style

<script type="text/javascript"><!--
google_ad_client = "pub-1234567890987654";
google_ad_slot = "1234567890";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script> 
<script type="text/javascript" src="path-to-google-script"></script>

Classic Style

<script type="text/javascript"><!--
google_ad_client = "pub-1234567890987654";
/* 336x280, created 8/6/09 */
google_ad_slot = "1234567890";
google_ad_width = 336;
google_ad_height = 280;
google_ad_format="336x280_as";
google_ad_type="text_image";
google_color_border="FFFFFF";
google_color_bg="FFFFFF";
google_color_link="2200CC";
google_color_url="000000";
google_color_text="777777";
//-->
</script> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:59:59+00:00Added an answer on May 13, 2026 at 9:59 pm

    What about something like this :

    $code = <<<STR
    <script type="text/javascript"><!--
    google_ad_client = "pub-1234567890987654";
    google_ad_slot = "1234567890";
    google_ad_width = 336;
    google_ad_height = 280;
    //-->
    </script> 
    <script type="text/javascript" src="path-to-google-script"></script>
    STR;
    
    if (preg_match('/google_ad_client = "([^"]+)";/', $code, $m)) {
        $google_ad_client = $m[1];
    }
    if (preg_match('/google_ad_slot = "([^"]+)";/', $code, $m)) {
        $google_ad_slot = $m[1];
    }
    
    var_dump($google_ad_client, $google_ad_slot);
    

    .
    And you’ll get as output :

    string 'pub-1234567890987654' (length=20)
    string '1234567890' (length=10)
    

    (Tested with the second portion of code, and seems to work OK too)

    Just as a precaution, you might also want to change the regexes to something like this, so accept the same kind of code without any space (or with any number of spaces) :

    if (preg_match('/google_ad_client\s*=\s*"([^"]+)"\s*;/', $code, $m)) {
        $google_ad_client = $m[1];
    }
    if (preg_match('/google_ad_slot\s*=\s*"([^"]+)"\s*;/', $code, $m)) {
        $google_ad_slot = $m[1];
    }
    

    And, just for fun, the same thing with only one regex, if you want to retrieve several pieces of informations :

    $results = array();
    if (preg_match_all('/(google_ad_client|google_ad_slot)\s*=\s*"([^"]+)"\s*;/', $code, $m)) {
        $count = count($m[1]);
        for ($i = 0 ; $i<$count ; $i++) {
            $results[$m[1][$i]] = $m[2][$i];
        }
    }
    
    // TODO : test is set (see isset) before using those
    var_dump($results['google_ad_client'], $results['google_ad_slot']);
    

    Which will give you the same output :

    string 'pub-1234567890987654' (length=20)
    string '1234567890' (length=10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.