Blorner

Thursday, June 17, 2010

Move JavaScript codes from a file to an other file

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);
?>

No comments:

Post a Comment

Art Works Expo