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.
This can be a complex operation. The steps:
glob)globreplacing the text as you go (preg_replace)file_put_contents)This sample code replaces all words in the
$wordsarray with a link tohttp://www.wordlink.com/<yourword>. If you need a different link for each word you’ll need to specify$replaceas an array using$1where you want the searched word to appear in the replacement (and change$replacein the regex to$replace[$i]).Also, the
globfunction below looks for all html files in the specified$filesDirdirectory. If you need something different you’re going to have to manually edit theglobpath 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
imodifier at the end of the pattern.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.