Page 1 of 1
Looking for something neater
Posted: Fri Sep 30, 2005 5:21 pm
by 127.0.0.1
Code: Select all
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $domain);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$code = curl_exec ($ch);
curl_close($ch);
$escapeSequences = array("\n", "\r");
$code = str_replace("<", "<", $code);
$code = str_replace(">", ">", $code);
$code = str_replace($escapeSequences, "<br>", $code);
echo $code;
Hi,
Above code is very simple.... it takes the html and displays it back out to the browser changing the < and > with < etc..
Now i am not that great with regular expressions... im wondering... i have 3 str_replace functions there... is there a more concise way to achive this with less lines of code?
Posted: Fri Sep 30, 2005 5:23 pm
by feyd
Posted: Fri Sep 30, 2005 5:26 pm
by 127.0.0.1
thanks....
I have been coding about 5 months and im always looking for better ways or shorter ways to be exact. I think it comes with practice
Posted: Fri Sep 30, 2005 5:33 pm
by 127.0.0.1
Code: Select all
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $_POST['url']);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$code = curl_exec ($ch);
curl_close($ch);
$escapeSequences = array("\n", "\r");
echo htmlentities(str_replace($escapeSequences, "<br><br>", $code));
I have it to this the problem is it groups the full html code as a whole with no formating and of course when i add br br in the str_replace it wont do much as htmlentitles is replacing < > etc
Any other ideas
Posted: Fri Sep 30, 2005 5:36 pm
by feyd
I added a reference to a second helpful function just after I posted originally..
Posted: Fri Sep 30, 2005 5:41 pm
by 127.0.0.1
hmmm.... mabye i have missed something i think you are reffering to nl2br ?
It doesnt seem to output the html to the browser.
as in this line of code <p> Hello </p> using nl2br outputs just hello ... i am looking for the <p> and </p> to be showing also with hello on the browser.
Posted: Fri Sep 30, 2005 5:44 pm
by feyd
you use
htmlentities() in combination with
nl2br() 
Posted: Fri Sep 30, 2005 5:47 pm
by 127.0.0.1
Thank you.... now i understand

works a treat
Posted: Fri Sep 30, 2005 5:51 pm
by 127.0.0.1
One last thing -- if you are ready to strangle me then its ok to do so....
I use curl.... is this some that you would use? I can tell you have been coding a while.
I always use curl?
Posted: Fri Sep 30, 2005 5:59 pm
by feyd
If I'm after the contents of a page request, I will try to use
file_get_contents() after checking to ensure that the url fopen wrappers are turned on. If they are off, I check to see if curl is available and use it, otherwise I attempt to use
fsockopen(). If that fails, I bail out throwing an error..