Dynadot โ€” .com Transfer

Some help with PHP

SpaceshipSpaceship
Watch

Bucaneiro

Account Closed
Impact
6
Hello,

I want to write a script that allows me to grab an website (like an iframe) but replacing some text strings. Ex.:

http://www.google.com/

Replacing: Advertising Programs - Business Solutions

To: Blablablabla - Asaasdsadasdas

Like a translator. How this can be done? Some example would be great. ;)
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
0
•••
I didn't understood very well... any sample source code or a tutorial with samples?
 
0
•••
Heres an incredibly basic bit of code:

PHP:
<?php

$file = file_get_contents("http://www.google.com");
$new = str_replace("blah", "blah", $file);
echo $new;

?>
 
0
•••
Mikor, how can i add more replacements? An array or something?

PS: How can i fetch only a div content or something?
PSยฒ: Sorry for asking too much guys. Many many thanks. :D
PSยณ: Is this right?

PHP:
$file = file_get_contents("http://www.google.com");

$search = array('word1', 'word2', 'word3', 'word4');
$replace = array('xxx', 'ccc', 'zzz', 'vvv');

$new = str_replace($search, $replace, $file);
echo $new;

How can i make this code faster? It's using too much CPU and resources. And waaaaay too slow.

PS4: How i can replace source-code? I'm trying to replace <img src=""> to <p>text</p> and it doesnt work.
 
Last edited:
0
•••
You should use cURL to grab the webpage. I have also shown how to grab the content within a specific division.

PHP:
<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.somesite.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Always best to have a timeout ;)
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$str = curl_exec();

// This is how to retrieve anything within a division
preg_match("/^<div id=\"division\">(.*?)<\/div>/is", $str, $matches);
$division_content = $matches[1];

echo $division_content;
?>

I haven't tested this but I am pretty sure it should work. :)
 
0
•••
Many thanks for the help, Dave. Could also tell me how i can replace source-code using the code i posted there? :)
 
0
•••
Also, i had some strange problems with the script:

Code:
[Wed Jul 25 16:42:58 2007] [error] PHP Warning:  Wrong parameter count for curl_exec() in e:\\easyphp1-8\\www\\test.php on line 11
[Wed Jul 25 16:42:58 2007] [error] PHP Notice:  Undefined offset:  1 in e:\\easyphp1-8\\www\\test.php on line 15
[Wed Jul 25 16:42:58 2007] [error] [client 127.0.0.1] File does not exist: e:/easyphp1-8/www/favicon.ico
[Wed Jul 25 16:42:59 2007] [error] PHP Warning:  Wrong parameter count for curl_exec() in e:\\easyphp1-8\\www\\test.php on line 11
[Wed Jul 25 16:42:59 2007] [error] PHP Notice:  Undefined offset:  1 in e:\\easyphp1-8\\www\\test.php on line 15
[Wed Jul 25 16:43:06 2007] [error] PHP Warning:  Wrong parameter count for curl_exec() in e:\\easyphp1-8\\www\\test.php on line 11
[Wed Jul 25 16:43:06 2007] [error] PHP Notice:  Undefined offset:  1 in e:\\easyphp1-8\\www\\test.php on line 15
[Wed Jul 25 16:44:35 2007] [error] PHP Warning:  Wrong parameter count for curl_exec() in e:\\easyphp1-8\\www\\test.php on line 11
[Wed Jul 25 16:44:35 2007] [error] PHP Notice:  Undefined offset:  1 in e:\\easyphp1-8\\www\\test.php on line 15
[Wed Jul 25 16:45:23 2007] [error] PHP Warning:  Wrong parameter count for curl_exec() in e:\\easyphp1-8\\www\\test.php on line 11
[Wed Jul 25 16:45:23 2007] [error] PHP Notice:  Undefined offset:  1 in e:\\easyphp1-8\\www\\test.php on line 15
 
0
•••
Sorry if I'm a little off topic but I'm curious to know what the advantage is of using curl over file_get_contents? Does it work very differently? It seems like it's doing essentially the same thing.
 
0
•••
For Lime : CURL is faster and more powerful.
Read comments on http://www.php.net/curl

For Bucaneiro :
About "replace"
"Simple" replace : str_replace
But if you want to replace inside tags, preg_replace is powerful
str_replace is faster than preg_replace

You will have a lot of help and examples if you read the explanations on www.php.net/str_replace and www.php.net/preg_replace
And read the comments at the bottom of the pages :)

For your script errors, I cannot help if you don't show the script creating the errors.
 
0
•••
The script is the one posted by Dave. :)
 
0
•••
ok, sorry...
curl_exec wants a parameter, the handle returned by curl_init().

Try with $str = curl_exec($ch);
 
0
•••
No luck.

Code:
[Wed Jul 25 19:58:30 2007] [error] PHP Notice:  Undefined offset:  1 in e:\\easyphp1-8\\www\\test.php on line 15
 
0
•••
I just corrected the first part, the source web page is now in $str

For the second part :
// This is how to retrieve anything within a division
preg_match("/^<div id=\"division\">(.*?)<\/div>/is", $str, $matches);
$division_content = $matches[1];

1. It's not for replacing strings, but just to extract them and put them in an array $match. You have to use preg_replace instead for replacing.
2. if you use the url http://www.somesite.com/, it won't find the tag <div id="division">. It was an example. so $match[1] wasn't defined.
3. Use preg_match with regular expressions, but you have to learn how to use them first, in order to do what you exactly want.
one link : http://www.webcheatsheet.com/php/regular_expressions.php
go to "replacing patterns" section
 
Last edited:
1
•••
Yeah do what proer has suggested. I should of made all that more clear in my post. Sorry! I was in a bit of a rush.
 
1
•••
Thanks for the help guys, but still, no success:

Code:
<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.gamefaqs.com/portable/gameboy/code/367023.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Always best to have a timeout ;)
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$str = curl_exec($ch);

// This is how to retrieve anything within a division
preg_match("/^<div class=\"pod cheat\">(.*?)<\/div>/is", $str, $matches);
$division_content = $matches[1]; 

echo $division_content;
?>
 
0
•••
Bucaneiro,
The script works, but it doesn't find anything...
use preg_match("/<div class=\"pod cheat\">(.*?)<\/div>/is", $str, $matches);
instead of
use preg_match("/^<div class=\"pod cheat\">(.*?)<\/div>/is", $str, $matches);

^ is to find a string at the beginning of the page, but you want to find it inside the page. Please read preg_match doc @ www.php.net/preg_match and regular expressions if you really want to go further.
Good luck
 
0
•••
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back