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 7079913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:43:48+00:00 2026-05-28T06:43:48+00:00

I have thousands of PHP pages which have a header and footer included using

  • 0

I have thousands of PHP pages which have a header and footer included using php, like

<?php include("header.php"); ?> //STATIC CONTENT HERE  <?php include("footer.php"); ?>

I want to implement auto keyword linking for certain keywords in the static text. But I can only add PHP codes to my header or footer files.

  • 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-28T06:43:49+00:00Added an answer on May 28, 2026 at 6:43 am

    This can be a complex operation. The steps:

    1. Get all the files whose words you want to replace (glob)
    2. Specify an array (or arrays) for the "find" and "replace" criteria
    3. Iterate over the files returned by glob replacing the text as you go (preg_replace)
    4. Write the new text (file_put_contents)

    This sample code replaces all words in the $words array with a link to http://www.wordlink.com/<yourword>. If you need a different link for each word you’ll need to specify $replace as an array using $1 where you want the searched word to appear in the replacement (and change $replace in the regex to $replace[$i]).

    Also, the glob function below looks for all html files in the specified $filesDir directory. If you need something different you’re going to have to manually edit the glob path yourself. Finally, the regular expression used only replaces whole words. i.e. if you wanted to replace the word super, the word superman will not have the word super replaced in the middle.

    Oh, and the replace is NOT case sensitive as per the i modifier at the end of the pattern.

    // specify where your static html files live
    $filesDir = '/path/to/my/html/files/';
    
    // specify where to save your updated files
    $newDir   = '/location/of/new/files/';
    
    // get an array of all the static html files in $filesDir
    $fileList = glob("$filesDir/*.html");
    
    $words    = array('super', 'awesome');
    $replace  = '<a href="http://www.wordlink.com/$1">$1</a>';
    
    // iterate over the html files.
    for ($i=0; $i < count($fileList); $i++) {
      $filePath = $filesDir . $fileList[$i];
      $newPath  = $newDir . $fileList[$i];
      
      $html     = file_get_contents($filePath);
      $pattern  = '#\b(' . str_replace('#', '\#', $words[$i]) . ')\b#i';
      $html     = preg_replace($pattern, $replace, $html);
      file_put_contents($newPath, $html);
      echo "$newpath file written\n";
    }
    

    Obviously, you need write-access to the new folder location. I would not recommend overwriting your original files. Translation:

    Always backup before doing anything crazy.

    P.S. the regexes are not UTF-8 safe, so if you’re dealing with international characters you’ll need to edit the regex pattern as well.

    P.P.S. I’m really being kind here because SO is not a code-for-free site. Don’t even think about commenting something like "it doesn’t work" when I try it 🙂 If it doesn’t fit your specifications, feel free to peruse the php manual for the functions involved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have decided to change all my /dyanmic.php?UID=... pages to /static/Name-From-DB . I have
Let's say I have thousands of users and I want to make the passwords
I have text files that have list of thousands of names like this DallasWebJobs
there have been hundreds if not thousands of posts concerning the use of PHP's
i have one php page with request data from other page using JSON i
I have a relatively large PHP-codebase (thousands of lines) for an application built with
I am brand new to PHP and I have thousands of records I need
I have created a php script that reads an excel sheet of thousands of
We have OCRed thousands of pages of newspaper articles. The newspaper, issue, date, page
I have thousands of psd files to save as png. The psd files are

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.