За последние 24 часа нас посетили 204554 программиста и 2113 роботов. Сейчас ищут 2055 программистов ...

Как разделить строку на отдельные слова?

Тема в разделе "PHP для новичков", создана пользователем Roomlife, 9 авг 2010.

  1. Roomlife

    Roomlife Активный пользователь

    С нами с:
    6 авг 2010
    Сообщения:
    34
    Симпатии:
    0
    PHP:
    1. <?
    2. class my_curl
    3. {
    4.     var $ch;
    5.     function init()
    6.     {
    7.         $this->ch = curl_init();
    8.         curl_setopt($this->ch, CURLOPT_COOKIESESSION, TRUE);
    9.         curl_setopt($this->ch, CURLOPT_HEADER, FALSE);
    10.         curl_setopt($this->ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    11.         curl_setopt($this->ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    12.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
    13.     }
    14.  
    15.     function close()
    16.     {
    17.         curl_close($this->ch);
    18.     }
    19.  
    20.     function get($url)
    21.     {
    22.         curl_setopt($this->ch, CURLOPT_HTTPGET, TRUE);
    23.         curl_setopt($this->ch, CURLOPT_URL, $url);
    24.         return curl_exec($this->ch);
    25.     }
    26.  
    27.     function post($url, $post)
    28.     {
    29.         curl_setopt($this->ch, CURLOPT_POST, TRUE);
    30.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
    31.         curl_setopt($this->ch, CURLOPT_URL, $url);
    32.         return curl_exec($this->ch);
    33.     }
    34. }
    35. echo '<table width=600 cellspacing=1 cellpadding=1 bgcolor=#efefef><tr bgcolor=#f8f8f8><td width=100><b>Персонаж</b></td><td><b>Статус</b></td><td><b>Онлайн</b></td><td><b>Местоположение</b></td><td><b>Бой</b></td></tr>';
    36.  
    37. $clans = fopen("http://www.obi-wan.ru/1/sostav3.txt","r");
    38. while ($data = fgetcsv($clans, 1000, "|"))
    39. {
    40. for ($c=0;$c<count($data);$c++)
    41. {
    42. if ($c < 0) {$data[$c] = "<font color=green>$data[$c]</font>";}
    43. if ($c == 1) {
    44. $curl = new my_curl();
    45. $curl->init();
    46. $result = $curl->get('http://www.neverlands.ru/modules/api/info.cgi?playerid='.$data[0].'&info=1');
    47. $curl->close();
    48.  
    49.  
    50.  
    51. ### Сюда надо написать функцию, которая будет читать результаты запроса $result и разделять его, чтобы каждое слово можно было использовать как $res[1] (первое слово), $res[2] (второе слово) и т.д. т.е. похожий метод как у fgetcsv()
    52.  
    53.  
    54. $nick='<a href="http://www.neverlands.ru/pinfo.cgi?'.$res[2].'" target=_blank><img src=http://image.neverlands.ru/chat/info.gif border=0></a>';
    55. $uroven=$res[3];
    56. $calign = '<img src=http://image.neverlands.ru/signs/chaoss.gif>';
    57. $znak = '<img src="http://image.neverlands.ru/signs/c269.gif" border=0>';
    58. $status=$res[8];
    59. if ($res[14] == '1') $onlain= 'Персонаж Онлайн'; else $onlain= 'Персонаж Оффлайн';
    60. $mesto=$res[15];
    61. if ($res[16] == '0') $boj = ''; else $boj= '<a href="http://www.neverlands.ru/logs.fcg?fid='.$res[16].'" target=_blank>В бою</a>';
    62. echo "<tr bgcolor=#ffffff><td width=100><b>$znak$calign$nick$uroven</b></td><td><b>$status</b></td><td><b>$onlain</b></td><td><b>$mesto</b></td><td><b>$boj</b></td></tr>";
    63. }
    64. }
    65. }
    66. fclose($clans);
    67. echo '</table>';
    68. ?>
    69.  
    То что закомментированно и является вопросом)
     
  2. WildZero

    WildZero Активный пользователь

    С нами с:
    24 фев 2010
    Сообщения:
    242
    Симпатии:
    0
    Адрес:
    Пермь
    $res = ("|",$result);
    Только первое слово будет с индексом 0, а не 1
     
  3. Roomlife

    Roomlife Активный пользователь

    С нами с:
    6 авг 2010
    Сообщения:
    34
    Симпатии:
    0
    WildZero
    Странно, но выдает Parse error: syntax error, unexpected ',' на строку с данным выражением
     
  4. Roomlife

    Roomlife Активный пользователь

    С нами с:
    6 авг 2010
    Сообщения:
    34
    Симпатии:
    0
    Тему можно закрыть. Решение нашел.
     
  5. [vs]

    [vs] Суперстар
    Команда форума Модератор

    С нами с:
    27 сен 2007
    Сообщения:
    10.559
    Симпатии:
    632
    $res = explode("|",$result);
     
  6. Roomlife

    Roomlife Активный пользователь

    С нами с:
    6 авг 2010
    Сообщения:
    34
    Симпатии:
    0
    [vs]
    вот именно это решение я уже и нашел)
     
  7. WildZero

    WildZero Активный пользователь

    С нами с:
    24 фев 2010
    Сообщения:
    242
    Симпатии:
    0
    Адрес:
    Пермь
    Заработался.. самое главное и забыл :)