How to change the source code and modify/parse a website?
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;
Thank You
Related posts:
- How to get(view) html source code of a website
- PHP: Parse Unparse String Array
- PHP source code encoding with ionCube PHP Encoder
- PHP: Read Write CSV
- jQuery: How to replace string, div content and image src?
- PHP: How to get stock quote data from Yahoo! Finance? (Complete Code and Tutorial)
- PHP: Simple and easy way to format URL string
- Left or Right Align your image, adsense code or other advertisement in wordpress
- PHP MaxMind GeoIP: Get country, city, postal code & much more by IP Address
- Random number, string generation in PHP
