V
V
Vladimir Shiklgruber2015-03-07 23:33:23
PHP
Vladimir Shiklgruber, 2015-03-07 23:33:23

When reading from a file, adds a space to php. How to fix?

Hey! Here I read a list of urls from the file and send curl requests to them (this is how it should work according to the idea) But in the end it doesn’t work. I started debugging the code in 20 lines ::blink.gif:: ::-ohmy.gif::
The error was that when reading where the line break automatically adds a space...

<?php
ini_set('display_errors', 1);
echo '<meta charset="utf-8">';
$f = fopen("bot.txt", "r");
while(!feof($f)) 
{ 
$bot_url= fgets($f);
if( $curl = curl_init() ) {
    curl_setopt($curl, CURLOPT_URL, 'http://'.$bot_url.'');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    $useragent="bot/0.9";
    curl_setopt($curl, CURLOPT_USERAGENT, $useragent); 
    curl_setopt($curl,CURLOPT_REFERER, "HTTP://bot/0.9"); 
    $out = curl_exec($curl);
    echo $out;
    curl_close($curl);
    }
} 
fclose($f);
?>

bot.txt
gogle.com
yandex.ru

It turns out that there is no space only on the last line. And where the transfer puts a space. the request is sent only to the last url from the file, it turns out that it also returns \n and I don’t know how to get rid of it. ATP in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2015-03-07
@mnjghgmjyt

php.net/manual/en/function.trim.php
$bot_url = trim( fgets( $f ) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question