Тема: Строки.
Показать сообщение отдельно
Непрочитано 28.10.2011, 22:42   #2
Аватар для PROGRAMMATOR
Администратор

По умолчанию Re: Строки.

Как пример.

PHP код:
    $string '
        501#
        A potion made from ground Red Herbs that restores ^000088about 45 HP^000000.
        ^ffffff_^000000
        Weight :^777777 7^000000
        #
        502#
        A potion made from ground Red and Yellow Herbs that restores ^000088about 105 HP^000000.
        ^ffffff_^000000
        Weight :^777777 10^000000
        #
    '
;
    
    
$lines explode(PHP_EOL$string);
    
$lines array_map('trim'$lines);
    
    
$item null;
    
$desc null;
    
$items = array();
    
    foreach (
$lines as $line) {
        if (
preg_match('#(^[\d]+)\##'$line$match)) {
            
$item $match[1];
            continue;
        }
        
        if (
preg_match('#^\##'$line)) {
            
$items[$item] = $desc;
            
$desc null;
            continue;
        }
        
        
$desc .= $line;
    }
    
    
var_dump($items); 
Код:
array
  501 => string 'A potion made from ground Red Herbs that restores ^000088about 45 HP^000000.^ffffff_^000000Weight :^777777 7^000000' (length=115)
  502 => string 'A potion made from ground Red and Yellow Herbs that restores ^000088about 105 HP^000000.^ffffff_^000000Weight :^777777 10^000000' (length=128)
__________________
composer require laravel/framework
yarn add vue
PROGRAMMATOR вне форума Отправить сообщение для PROGRAMMATOR с помощью ICQ Отправить сообщение для PROGRAMMATOR с помощью Skype™ Ответить с цитированием
Сказали спасибо: