0

Столкнулась с проблемой.

Есть текстовый файл:

item({'id':'16', 'title':'Title1', 'date':'2018-10-31 10:16:26', 'content':'Content1'});
item({'id':'18', 'title':'Title2', 'date':'2018-10-31 10:16:42', 'content':'Content2'});
item({'id':'20', 'title':'Title3', 'date':'2018-10-31 10:17:02', 'content':'Content3'});

Мне необходимо получить номер строки, в которую входит подстрока "'id':'18'".

2 ответа 2

0
$data = file_get_contents('file_name.txt');
$str = explode("\n", $data);

foreach($str as $id => $item) { 
    if(strpos($item, "\"'id':'18'\"") !== false) $result = $id+1;
}

echo "Номер строки - ".$result;
0

Спасибо, код работает, но не с этой регуляркой...

Вот так заработало:

$data = file_get_contents('news.js');
$str = explode("\n", $data);

foreach($str as $id => $item) { 
if(strpos($item, '\'id\':\'18\'') !== false) $result = $id+1;
}
echo "Номер строки - ".$result;

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.