Кемеровские программисты php общаются здесь
-
Anonymous
Как избежать пустых элементов, разбив строку на один или несколько последовательных пробелов? [дубликат]
Сообщение
Anonymous »
Я использую file_get_contents для ответа с удаленного адреса и развертывание для создания массива данных.
Для этого я использую следующий код:
Код: Выделить всё
function test() {
$project_key ='fg54gth5k7';
$postdata = http_build_query(
array(
'code' => $project_key
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://test.com/', false, $context);
return $result;
}
$res = test();
echo $res;
$part = explode(' ', $res);
var_dump($part);
echo $res возвращает строку:
Код: Выделить всё
3434343 http://test.com/index.php?r=site/stepone
&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 OK68 OK Redirect
http://test.com/index.php?r=site/stepone&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 0
var_dump() возвращает массив
Код: Выделить всё
array(5) {
[0]=> string(8) "3434343"
[1]=> string(0) ""
[2]=> string(0) ""
[3]=> string(110) "http://test.com/index.php?r=site/stepone
&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 OK68 OK Redirect"
[4]=> string(100) "http://test.com/index.php?
r=site/stepone&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 0 "
Скажите, пожалуйста, почему я получаю неправильный массив?
массив должен быть следующим:
Код: Выделить всё
array(10) {
[0]=> string(8) "3434343"
[1]=> string(0) ""
[2]=> string(0) ""
[3]=> string(110) "http://test.com/index.php?r=site/stepone
&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6"
[4]=> string(4) "OK68 OK Redirect"
[5]=> string(2) "OK Redirect"
[6]=> string(8) "Redirect"
[7]=> string(100) "http://test.com/index.php?
r=site/stepone&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6"
[8]=> string(1) "0"
[9]=> string(0) ""
Подскажите пожалуйста, где ошибка?
Подробнее здесь:
https://stackoverflow.com/questions/146 ... cutive-spa
1737111965
Anonymous
Я использую file_get_contents для ответа с удаленного адреса и развертывание для создания массива данных.
Для этого я использую следующий код:
[code]function test() {
$project_key ='fg54gth5k7';
$postdata = http_build_query(
array(
'code' => $project_key
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://test.com/', false, $context);
return $result;
}
$res = test();
echo $res;
$part = explode(' ', $res);
var_dump($part);
[/code]
[b]echo $res возвращает строку:[/b]
[code]3434343 http://test.com/index.php?r=site/stepone
&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 OK68 OK Redirect
http://test.com/index.php?r=site/stepone&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 0
[/code]
[b]var_dump() возвращает массив[/b]
[code]array(5) {
[0]=> string(8) "3434343"
[1]=> string(0) ""
[2]=> string(0) ""
[3]=> string(110) "http://test.com/index.php?r=site/stepone
&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 OK68 OK Redirect"
[4]=> string(100) "http://test.com/index.php?
r=site/stepone&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6 0 "
[/code]
[b]Скажите, пожалуйста, почему я получаю неправильный массив?[/b]
[b]массив должен быть следующим:[/b]
[code]array(10) {
[0]=> string(8) "3434343"
[1]=> string(0) ""
[2]=> string(0) ""
[3]=> string(110) "http://test.com/index.php?r=site/stepone
&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6"
[4]=> string(4) "OK68 OK Redirect"
[5]=> string(2) "OK Redirect"
[6]=> string(8) "Redirect"
[7]=> string(100) "http://test.com/index.php?
r=site/stepone&temptoken=c68c0ae1cece433fe5d6f6578cc0a9b6"
[8]=> string(1) "0"
[9]=> string(0) ""
[/code]
[b]Подскажите пожалуйста, где ошибка?[/b]
Подробнее здесь: [url]https://stackoverflow.com/questions/14624886/how-to-avoid-empty-elements-by-splitting-a-string-on-one-or-more-consecutive-spa[/url]