Return to Snippet

Revision: 57435
at May 26, 2012 02:13 by crypticsoft

Initial Code
## Convert from PHP to ASP extensions with SSI includes ##

getFiles() - Get all php files from root / includes directory
stripIncludes() - Regex php syntax and includes for SSI
saveFile(filename) - Save string to (.asp) file extension while preserving the filename

## Concept - 
1) function getFiles( directory ) - all php files from directory and loop through each
2) function parseDir( directory ) - copy file to .asp extension and remove the .php version
- replace php includes with SSI then save file

	## Examples:
	//parse includes folder '/inc' or use $_SERVER['DOCUMENT_ROOT']
	parseDir( dirname( __FILE__ ) . '/inc/' );
	//parse root
	parseDir( dirname( __FILE__ ) );

	//copy all of your php source into another directory, replace 'asp_directory' and set to your export path
	parseDir( $_SERVER['DOCUMENT_ROOT'] . '/asp_directory/' );
	parseDir( $_SERVER['DOCUMENT_ROOT'] . '/asp_directory/inc/' );

	function getFiles($dir){
		//start with root files
		if ($handle = opendir($dir)) {
			$files = '';
			//loop through all of the files		
			while (false !== ($file = readdir($handle))) {
				if($file!='.'&&$file!='..'&&stripos($file,'.php')){ //all php files
		return $files;	

	function parseDir($dir){

	foreach( getFiles( $dir ) as $file ){
	if( $file != 'convert.php' && $file != '' ){ //parse all files except this one
		$fname = str_replace('.php','.asp',$file);//strip off extension to add in fwrite
		$fpath = $dir . $fname;//new filename and path
		$orig = $dir . $file;

		//copy from php to asp extension
		if( copy($orig, $fpath) ) {
			unlink($orig);//remove the php version
			print "Error: unable to copy file " . $orig . " - to - " . $fpath . " <br>check file permissions";

		//remove the old php version

		//read the file contents into a string for replacements
		$file_str = file_get_contents( $fpath );
		//replace the php include statements with SSI virtual includes
		$file_str = str_replace( "<? include('", '<!--#include virtual="', $file_str ); // 'virtual' = relative path
		$file_str = str_replace( ".php'); ?>", '.asp"-->', $file_str ); //closing SSI tags
		$file_str = str_replace( ".php", ".asp", $file_str ); //replace all .php link references to .asp
		//if the file is writeable then proceed
		if( is_writeable( $fpath ) ){
			//write the new string to the .asp file - just as it should be (plain text)
			$fp = fopen( $fpath, 'w' );
			fwrite( $fp, $file_str ); //write the new file

			print 'Error: Cannot write to directory, check permissions on' . $fpath;
		}//end if

	}//end if file
		print "parsed: " . $fpath . "<br />";
	}//end foreach

	}//end parseDir


Initial URL

Initial Description
For those cases where you want to code in PHP but need to deploy to ASP. It doesn't account for php logic but only converts the php include() statements into SSI includes. Could be useful for converting basic websites from php to asp, rare but it happens :)

Initial Title
Parse PHP files into ASP extensions

Initial Tags
php, convert

Initial Language