PHP jQuery Update
When you are running 30 or so websites updating something like (awesome) jQuery lib can be pain in the a*, well, no longer I hope so.
By executing 2 lines of code you can update your jQuery to latest or whichever you wish (as long as it exists

), just make sure you name your file like "jquery.js" without version in your HTML files.
Yes, I know there is JS Google API for this
@todo update on construct...
/**
* @author Webarto.com
* @copyright 2011
* @url http://webarto.com/76/php-jquery-update
* @version 0.1
*/
class jQuery{
private $api_url = "http://code.google.com/apis/libraries/devguide.html"; // Google API Guide
private $api_path = "https://ajax.googleapis.com/ajax/libs/jquery/"; // jQuery lib path (without file specified)
private $api_versions = 'var versions = \[(.*?)];'; // Version RegEx
public function update($file = "jquery.js", $version = "latest", $minified = true){
$data = $this->curl($this->api_url); //Fetch HTML
$regex = $this->api_versions;
$x = preg_match("#$regex#is", $data, $matches); // Parse jQuery versions from HTML
if(!$x) die("jQuery lib not found!"); // Versions not found, abort!
$versions = preg_replace("#[^0-9.,]#", "", $matches[1]); //Convert JS array to PHP array
$versions = explode(",", $versions);
//Check if specified version if found, otherwise use latest
if($version != "latest"){
if(!in_array($version, $versions)){
die("jQuery version $version not found!");
}
}else{
$version = end($versions);
}
//Minified or Uncompressed?
$compression = ($minified == true)? "jquery.min.js": "jquery.js";
//Fetch jQuery lib
$data = $this->curl($this->api_path."/".$version."/".$compression);
//Check if previous operation returned anything
if(!empty($data)){
if(file_exists($file)) $from = $this->from($file); //Get local jQuery version, if found.
if(empty($from)) $from = "N/A";
$this->write($data, $file); //Write data to file
if(file_exists($file)) return "jQuery lib updated from $from to $version!"; // Yay!
}else{
return "jQuery lib updating failed!"; // Boooo!
}
}
private function write($data, $file){
$f = fopen($file, "w+");
fwrite($f, $data);
fclose($f);
}
private function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
curl_close ($ch);
}
private function from($file){
$data = file_get_contents($file);
$x = preg_match("#v([0-9.]+)#is", $data, $matches);
if($x){
return $matches[1];
}else{
return "N/A";
}
}
}
Various examples:
$jQuery = new jQuery();
echo $jQuery->update(); // jQuery lib updated from 1.4.2 to 1.5.0!
echo $jQuery->update("jquery.js", "latest", true); // jQuery lib updated from 1.4.4 to 1.5.0!
echo $jQuery->update("jquery.min.js", "latest", true); // jQuery lib updated from N/A to 1.5.0!
echo $jQuery->update("jquery.min.js", "1.4.4", true); // jQuery lib updated from 1.3.2 to 1.4.4!
echo $jQuery->update("jquery.js", "1.4.4", false); // jQuery lib updated from N/A to 1.4.4!