Home » PHP

How to change the source code and modify/parse a website?

14 November 2008 4,828 views Popularity: 9% Share/Bookmark

email

Earlier I had written an article on How to get(view) html source code of a website . I had created an application and that would grab the html code of any website. In this article, I will be writing on how you can modify the looks of a website by changing the html source code after you grab it.

At first the html source code getting part. I have commented in the code for better understanding.

// storing the domain name posted in a variable
$domain = $_POST['domain'];

// fopen(string filename, "r") opens for reading only; place the file pointer at the beginning of the file.
$handle = fopen("http://$domain","r");

$contents = '';

// feof() Tests for end-of-file on a file pointer
// Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.
while (!feof($handle)) {

// string fread ( resource handle, int length )
// fread() reads up to length bytes from the file pointer referenced by handle. Reading stops when up to length bytes have been read, EOF (end of file) is reached
$contents .= fread($handle, 8192);
}
//var_dump($contents);

fclose($handle);

Now, the modification in the source code part. I have used ereg_replace() function for the modification.

// You have the html source code in the variable $code
$code = $_POST['code'];

// $present consits of the text present in the html source code that is to be replaced
$present = $_POST['present'];

// $replace consits of the text that you are going to replace $present with
$replace = $_POST['replace'];

// string ereg_replace ( string pattern, string replacement, string string )
// This function scans string for matches to pattern, then replaces the matched text with replacement.
// The modified string is returned. (Which may mean that the original string is returned if there are no matches to be replaced.)
$code = ereg_replace($present, $replace , $code);

// string stripslashes ( string str )
// Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).
// This is useful if you are using slashes in $present or in $replace
echo stripslashes($code);

exit;

Download | View Demo

Thank You

Related posts:

  1. How to get(view) html source code of a website
  2. PHP: Parse Unparse String Array
  3. PHP source code encoding with ionCube PHP Encoder
  4. PHP: Read Write CSV
  5. jQuery: How to replace string, div content and image src?
  6. PHP: How to get stock quote data from Yahoo! Finance? (Complete Code and Tutorial)
  7. PHP: Simple and easy way to format URL string
  8. Left or Right Align your image, adsense code or other advertisement in wordpress
  9. PHP MaxMind GeoIP: Get country, city, postal code & much more by IP Address
  10. Random number, string generation in PHP
  • Mike Jefferson

    Hi
    I was looking to edit a website and was wondering if you could help me by showing me the steps, would be greatly appreciated.

    Mr Jefferson