[Регулярки & Mod_Rewrite] Задай вопрос, получи ответ.

Discussion in 'PHP' started by .:EnoT:., 19 Nov 2008.

Thread Status:
Not open for further replies.
  1. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Как из этой строки:
    PHP:
    "55":{"id":19410,"name":"\u0413\u0430\u0433\u0430\u0432\u0442\u0430\u044d","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":472,"oy":147,"offer":"wood","demand":"stone","mood":100,"relation_status":1,"ratio":1.25,"loot":1342460846,"lootable_human":"16.07.12 \u0432 21:47","looted":1342460546},"56":{"id":19411,"name":"\u041a\u043e\u0433\u0430\u0445\u0438\u0434\u0440\u044d\u0439","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":680,"oy":234,"offer":"stone","demand":"wood","mood":100,"relation_status":0,"ratio":1.25}
    выбрать значения "id", "name" и "relation_status"?

    PHP:
    preg_match_all ('#"id":([0-9]+),"name":"([^"]+)#i')
    Так я получаю Id и name...
    А вот как еще здесь же получить "relation_status"???

    Json_decode не подходит...
     
  2. krypt3r

    krypt3r Elder - Старейшина

    Joined:
    27 Apr 2007
    Messages:
    1,507
    Likes Received:
    389
    Reputations:
    101
    Подходит, если подойти к тексту правильно:
    PHP:
    <?php
      $str 
    '{"55":{"id":19410,"name":"\u0413\u0430\u0433\u0430\u0432\u0442\u0430\u044d","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":472,"oy":147,"offer":"wood","demand":"stone","mood":100,"relation_status":1,"ratio":1.25,"loot":1342460846,"lootable_human":"16.07.12 \u0432 21:47","looted":1342460546},"56":{"id":19411,"name":"\u041a\u043e\u0433\u0430\u0445\u0438\u0434\u0440\u044d\u0439","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":680,"oy":234,"offer":"stone","demand":"wood","mood":100,"relation_status":0,"ratio":1.25}}';
      
    print_r (json_decode ($strTRUE));
    ?>
    Вывод:
    Code:
    Array
    (
        [55] => Array
            (
                [id] => 19410
                [name] => Гагавтаэ
                [dir] => n
                [expansion_stage] => 3
                [x] => 788
                [y] => 648
                [ox] => 472
                [oy] => 147
                [offer] => wood
                [demand] => stone
                [mood] => 100
                [relation_status] => 1
                [ratio] => 1.25
                [loot] => 1342460846
                [lootable_human] => 16.07.12 в 21:47
                [looted] => 1342460546
            )
    
        [56] => Array
            (
                [id] => 19411
                [name] => Когахидрэй
                [dir] => n
                [expansion_stage] => 3
                [x] => 788
                [y] => 648
                [ox] => 680
                [oy] => 234
                [offer] => stone
                [demand] => wood
                [mood] => 100
                [relation_status] => 0
                [ratio] => 1.25
            )
    
    )
    
     
  3. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Это то да, но мне нужно парсить ответ сервера, а он мне присылает вот что:
    http://narod.ru/disk/56515222001.d06c63ea8...2/Json.doc.html

    Вот как мне оттуда Json выбрать? Поэтому и пытаюсь регулярку сварганить...
     
  4. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Попробую по другому:

    Допустим есть несколько строк такого вида:
    PHP:
    "50":{"id":16600,"name":"\u0420\u043e\u0441\u0433\u0430\u0432","dir":"n","expansion_stage":3,"x":775,"y":639,"ox":181,"oy":236,"offer":"iron","demand":"wood"}

    "54":{"id":19409,"name":"\u0420\u043e\u0441\u043d\u043e\u0441\u043a\u043e\u043d\u043e\u0441","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":629,"oy":170,"offer":"stone","demand":"wood","mood":100,"relation_status":1,"ratio":1.25,"loot":1342173457,"lootable_human":"13.07.12 \u0432 13:57","looted":1342173157}

    "52":{"id":18546,"name":"\u0413\u0430\u0432\u043d\u044d\u0439\u043a\u043e\u0434\u0440\u044d\u0439","dir":"n","expansion_stage":4,"x":776,"y":656,"ox":700,"oy":200,"offer":"iron","demand":"wood"}

    "53":{"id":18550,"name":"\u041d\u043e\u0441\u0445\u0438\u0433\u0430\u0432\u0440\u043e","dir":"n","expansion_stage":4,"x":776,"y":656,"ox":452,"oy":92,"offer":"iron","demand":"wood"}

    "57":{"id":19412,"name":"\u0422\u0430\u0445\u0438\u0433\u0430","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":694,"oy":296,"offer":"stone","demand":"wood","mood":100,"relation_status":0,"ratio":1.25}
    Как мне выбрать то, что находится между {.....}, но только те, где встречается relation_status...

    ???

    Т.е. оставить только
    PHP:
    {"id":19409,"name":"\u0420\u043e\u0441\u043d\u043e\u0441\u043a\u043e\u043d\u043e\u0441","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":629,"oy":170,"offer":"stone","demand":"wood","mood":100,"relation_status":1,"ratio":1.25,"loot":1342173457,"lootable_human":"13.07.12 \u0432 13:57","looted":1342173157}

    {
    "id":19412,"name":"\u0422\u0430\u0445\u0438\u0433\u0430","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":694,"oy":296,"offer":"stone","demand":"wood","mood":100,"relation_status":0,"ratio":1.25}
     
  5. Gifts

    Gifts Green member

    Joined:
    25 Apr 2008
    Messages:
    2,494
    Likes Received:
    807
    Reputations:
    614
    Seganapa
    PHP:
    $data '"50":{"id":16600,"name":"\u0420\u043e\u0441\u0433\u0430\u0432","dir":"n","expansion_stage":3,"x":775,"y":639,"ox":181,"oy":236,"offer":"iron","demand":"wood"}
    "54":{"id":19409,"name":"\u0420\u043e\u0441\u043d\u043e\u0441\u043a\u043e\u  043d\u043e\u0441","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":629,"oy":170,"offer":"stone","demand":"wood","mood":100,"relation_status":1,"ratio":1.25,"loot":1342173457,"lootable_human":"13.07.12 \u0432 13:57","looted":1342173157}
    "52":{"id":18546,"name":"\u0413\u0430\u0432\u043d\u044d\u0439\u043a\u043e\u  0434\u0440\u044d\u0439","dir":"n","expansion_stage":4,"x":776,"y":656,"ox":700,"oy":200,"offer":"iron","demand":"wood"}
    "53":{"id":18550,"name":"\u041d\u043e\u0441\u0445\u0438\u0433\u0430\u0432\u  0440\u043e","dir":"n","expansion_stage":4,"x":776,"y":656,"ox":452,"oy":92,"offer":"iron","demand":"wood"}
    "57":{"id":19412,"name":"\u0422\u0430\u0445\u0438\u0433\u0430","dir":"n","expansion_stage":3,"x":788,"y":648,"ox":694,"oy":296,"offer":"stone","demand":"wood","mood":100,"relation_status":0,"ratio":1.25}'
    ;
    $data explode("\n"$data);

    $result = array();
    foreach(
    $data as $row)
    {
        if (
    stripos($row'relation_status')===false) continue;
        
    $tmp explode(':'$row2);
        
    $result[] = $tmp[1];
    }
    var_dump($result);
     
    _________________________
  6. Seganapa

    Seganapa New Member

    Joined:
    8 Jun 2012
    Messages:
    74
    Likes Received:
    0
    Reputations:
    0
    Так работает...

    А как их выбрать отсюда (должно получится 8 строчек, где есть relation_status):
    http://dl.dropbox.com/u/70752478/Server Request.txt

    На выходе нужно получить:
    PHP:
    {\"id\":19409,\"name\":\"\\u0420\\u043e\\u0441\\u043d\\u043e\\u0441\\u043a\\u043e\\u043d\\u043e\\u0441\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":629,\"oy\":170,\"offer\":\"stone\",\"demand\":\"wood\",\"mood\":100,\"relation_status\":1,\"ratio\":1.25,\"loot\":1342173457,\"lootable_human\":\"13.07.12 \\u0432 13:57\",\"looted\":1342173157}
    {\"id\":19410,\"name\":\"\\u0413\\u0430\\u0433\\u0430\\u0432\\u0442\\u0430\\u044d\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":472,\"oy\":147,\"offer\":\"wood\",\"demand\":\"stone\",\"mood\":100,\"relation_status\":1,\"ratio\":1.25,\"loot\":1342460846,\"lootable_human\":\"16.07.12 \\u0432 21:47\",\"looted\":1342460546}
    {\"id\":19411,\"name\":\"\\u041a\\u043e\\u0433\\u0430\\u0445\\u0438\\u0434\\u0440\\u044d\\u0439\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":680,\"oy\":234,\"offer\":\"stone\",\"demand\":\"wood\",\"mood\":100,\"relation_status\":0,\"ratio\":1.25}
    {\"id\":19412,\"name\":\"\\u0422\\u0430\\u0445\\u0438\\u0433\\u0430\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":694,\"oy\":296,\"offer\":\"stone\",\"demand\":\"wood\",\"mood\":100,\"relation_status\":0,\"ratio\":1.25}
    {\"id\":19413,\"name\":\"\\u0420\\u043e\\u0442\\u0430\\u0440\\u043e\\u0441\\u044d\",\"dir\":\"n\",\"expansion_stage\":4,\"x\":788,\"y\":648,\"ox\":278,\"oy\":208,\"offer\":\"stone\",\"demand\":\"iron\",\"mood\":100,\"relation_status\":1,\"ratio\":1.25,\"loot\":1342460843,\"lootable_human\":\"16.07.12 \\u0432 21:47\",\"looted\":1342460543}
    {\"id\":19414,\"name\":\"\\u0414\\u043e\\u043d\\u043e\\u0441\\u0433\\u0430\\u0444\\u043e\\u0441\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":228,\"oy\":265,\"offer\":\"wood\",\"demand\":\"iron\",\"mood\":100,\"relation_status\":1,\"ratio\":1.25,\"loot\":1342173456,\"lootable_human\":\"13.07.12 \\u0432 13:57\",\"looted\":1342173156}
    {\"id\":19415,\"name\":\"\\u041d\\u044d\\u0439\\u0440\\u043e\\u0441\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":589,\"oy\":365,\"offer\":\"iron\",\"demand\":\"wood\",\"mood\":100,\"relation_status\":0,\"ratio\":1.25}
    {\"id\":19416,\"name\":\"\\u0421\\u0442\\u0440\\u0430\\u0439\\u043d\\u043e\\u0441\\u043a\\u043e\\u0443\\u0441\",\"dir\":\"n\",\"expansion_stage\":3,\"x\":788,\"y\":648,\"ox\":772,\"oy\":274,\"offer\":\"stone\",\"demand\":\"wood\",\"mood\":100,\"relation_status\":0,\"ratio\":1.25}
    Такое возможно?
     
    #1826 Seganapa, 18 Jul 2012
    Last edited: 18 Jul 2012
  7. Melfis

    Melfis Elder - Старейшина

    Joined:
    25 Apr 2011
    Messages:
    505
    Likes Received:
    105
    Reputations:
    53
    Так сервер этой игры не может отсылать данные как представляешь ты нам их. Прекрати долбиться х**м об стену и начни понимать что ты делаешь. Все данные в формате json, тебе уже объяснили как с ним работать и не надо никаких регулярок использовать.
     
  8. shadowrun

    shadowrun Banned

    Joined:
    29 Aug 2010
    Messages:
    842
    Likes Received:
    170
    Reputations:
    84
    Приветствую, нужно спарсить строку вида
    value="98edd7aab0397c03c1526a0ed00c6a0d
    Но результат получить без value="
     
  9. krypt3r

    krypt3r Elder - Старейшина

    Joined:
    27 Apr 2007
    Messages:
    1,507
    Likes Received:
    389
    Reputations:
    101
    /value="([0-9a-f]{32})"/
     
    1 person likes this.
  10. .vk

    .vk Banned

    Joined:
    5 Feb 2012
    Messages:
    3
    Likes Received:
    1
    Reputations:
    0
    Не понял если честно :)
    Решил так:
    PHP:
    $message preg_replace("/(<\/div>.*?<div)/i"'</div><div'$message);
    $message preg_replace("/(\{.*?<div)/i"'<div'$message);
    $message preg_replace("/(<\/div>[^<div].*})/i"'</div>'$message);
    { - начало, } - конец изначального $message, выделить начало и конец с помощью ^ и $ почему-то не получилось
     
    1 person likes this.
  11. Dzhum

    Dzhum Banned

    Joined:
    27 Nov 2010
    Messages:
    0
    Likes Received:
    0
    Reputations:
    0
    Подскажите пожалуйста регулярку, из участка кода

    <a href="javascript:void(0);" onclick="_cgo('go.php?u=aHR0cDovL3NlYXJ&amp;qq=Y2FzaW5v&amp;stime=1343249248');" target="_self" onmouseout="window.status='';return true;" onmouseover="window.status='www.site.com';return true;">text</a>

    нужно извлечь только ссылку

    go.php?u=aHR0cDovL3NlYXJ&amp;qq=Y2FzaW5v&amp;stime=1343249248

    таких фрагментов кода в исходнике 15, т. е. получить 15 ссылок в массив. Спасибо.
     
  12. Melfis

    Melfis Elder - Старейшина

    Joined:
    25 Apr 2011
    Messages:
    505
    Likes Received:
    105
    Reputations:
    53
    preg_match_all
    /_cgo\('(go.php\?u=.+?)'\)/
     
  13. OnArs

    OnArs Banned

    Joined:
    1 Aug 2008
    Messages:
    211
    Likes Received:
    13
    Reputations:
    1
    Со страницы: http://lib.rus.ec/b/386529

    Необходимо получить:
    - Текст Аннотации

    Пробую так:
    PHP:
    /Аннотация<\/h2>(.*)<p><p>(.+)<\/p>/siU
    Но ничего не выходит :(
     
  14. Rastamanka

    Rastamanka Elder - Старейшина

    Joined:
    26 Nov 2008
    Messages:
    429
    Likes Received:
    11
    Reputations:
    7
    как спарсить значение между первым и шестым <td></td> ?
    В данном примере это
    1)
    2)
    Текст между всеми TD постоянно меняется.
     
  15. daniel777

    daniel777 Elder - Старейшина

    Joined:
    8 Jul 2010
    Messages:
    517
    Likes Received:
    90
    Reputations:
    37
    Исходный текст в $str
    PHP:
    preg_match_all("#<td>(.*?)<\/td>#"$str$td);

    echo 
    $td[1][0];
    echo 
    $td[1][5];
    Во втором теге TD надо вытаскивать ссылку?
    Если да, то так:
    PHP:
    preg_match_all("#<td>(.*?)<\/td>#"$str$td);

    preg_match('/<a href=\"(.*?)\"/'$td[1][5], $l);

    echo 
    $td[1][0];
    echo 
    $l[1];
     
    #1835 daniel777, 28 Jul 2012
    Last edited: 28 Jul 2012
  16. Rastamanka

    Rastamanka Elder - Старейшина

    Joined:
    26 Nov 2008
    Messages:
    429
    Likes Received:
    11
    Reputations:
    7
    Как двумя регулярками сделать я знаю. Надо одной. Да и просто парсить данные между <td></td> не надо т.к. таких строк порядка 1000-5000 по этому и указаны <tr> </tr> были
     
  17. daniel777

    daniel777 Elder - Старейшина

    Joined:
    8 Jul 2010
    Messages:
    517
    Likes Received:
    90
    Reputations:
    37
    Когда найдете более простое и правильное решение, уведомите пожалуйста меня буду очень благодарен:)
     
  18. Melfis

    Melfis Elder - Старейшина

    Joined:
    25 Apr 2011
    Messages:
    505
    Likes Received:
    105
    Reputations:
    53
    Code:
    /<td>(.+?)<\/td>(?:.+?<td>){4}<a\shref="(.+?)"/
     
  19. AnGeI

    AnGeI Elder - Старейшина

    Joined:
    8 Dec 2008
    Messages:
    395
    Likes Received:
    79
    Reputations:
    16
    Есть текст - многострочный, в котором есть {текст1|текст2|текст3}.
    Например:
    Code:
    blablabla
    11{msg1|txt|msg2}111111{next|go|to}11111111
    Нужна регулярка, которая образует массив:
    [0] => {msg1|txt|msg2}
    [1] => {next|go|to}
     
  20. Melfis

    Melfis Elder - Старейшина

    Joined:
    25 Apr 2011
    Messages:
    505
    Likes Received:
    105
    Reputations:
    53
    /{.+?\|.+?\|.+?}/
     
Thread Status:
Not open for further replies.