All pastes #59955 Raw Edit

Links.functions

public php v1 · immutable
#59955 ·published 2006-05-30 16:13 UTC
rendered paste body
<?php	function findLinks($file)	{		if( is_file($file) && is_readable($file) )		{				$links = array();				// get whole file into a string			if( $strfile = file_get_contents($file) )			{				while( $trim = stristr( $strfile, "href=" ) )				{					// strip "href=" from link					$ltrim   = substr( $trim,  5   );						// find out how links are enclosed					switch( $ltrim[0] )					{						case '"':			// "http..."							$delim = '"';							$ltrim   = substr( $ltrim, 1 );							break;						case "'":							$delim = "'";	// 'http...'							$ltrim   = substr( $ltrim, 1 );							break;						default:							$delim = ' ';	// http... (no enclosure)					}										// If no enclosure is found, not even a space, then use '>'					$npos	 = strpos( $ltrim,  $delim );					$opos    = strpos( $ltrim,  ">"    );					if( $npos > $opos || $npos == false )						$npos = $opos;										// finish trimming using the position of the end delimiter					$rtrim   = substr( $ltrim, 0, $npos );										// add link to array					$links[] = $rtrim;										// update the string to be after this link					$strfile = substr( $ltrim, $npos+1 );				}				return $links;			}		}		return false;	}	function replaceLinks($file, $oldlinks, $newlinks)	{		if( is_file($file) && is_writeable($file) )		{				// make sure we are supplied a newlink for every oldlink			if ( count($oldlinks) == count($newlinks) )			{				// open file for reading and writing				$fp = fopen($file, 'r+');								if( $fp )				{										// get whole file into a string					if( $strfile = fread($fp, filesize($file) ) )					{						// replace links						$strfile = str_replace( $oldlinks, $newlinks, $strfile );												// pointer goes back to the beginning of file						rewind($fp);												// overwrite file with updated links						$byteswritten = fwrite( $fp, $strfile, strlen($strfile) );												fclose( $fp );												return $byteswritten;					}				}			}		}		return false;	}?>