An example to move the JavaScript codes from a file to an other file:

<?php
function moveJavascriptCodes($fromFile,$toFile){
    $fileText = file_get_contents($fromFile);
 
    preg_match_all("#<script\b[^>]*>(.*?)</script>#si",$fileText,$javascriptCodes,PREG_SET_ORDER);
 
    $codes = array();
 
    foreach($javascriptCodes as $javascriptCode){
        $codes[] = $javascriptCode[1];
    }
 
    if(file_put_contents($toFile,implode("\n",$codes))===false){
        return false;
    }
 
    return true;
}
 
$fromFile = "totalCode.php"; //The file from which JavaScript code will be got
$toFile = "javascriptCode.js"; //The file into which JavaScript code will be set
 
moveJavascriptCodes($fromFile,$toFile);
?>
Share

1 Comment on Move JavaScript codes from a file to an other file

  1. very good & helpful tricks to use when you need, thank for sharing

Leave a Reply

Info