Looking for something neater

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Looking for something neater

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post 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
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I added a reference to a second helpful function just after I posted originally..
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you use htmlentities() in combination with nl2br() ;)
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post by 127.0.0.1 »

Thank you.... now i understand :) works a treat
127.0.0.1
Forum Newbie
Posts: 22
Joined: Mon Sep 12, 2005 8:59 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply