开发者

Search String line-by-line backwards Bottom to Top

开发者 https://www.devze.com 2023-02-25 21:47 出处:网络
I have a file with the following contents. I would like to search string \"13030115...\" i.e. value before \"#\". But search should be performed backwards i.e. Bottom to Top. How can I do this in PHP

I have a file with the following contents. I would like to search string "13030115..." i.e. value before "#". But search should be performed backwards i.e. Bottom to Top. How can I do this in PHP ?

1303011581#user: textMsg
1303011582#user: 开发者_如何学运维textMsg
1303011583#user: textMsg
1303011584#user: textMsg
1303011585#user: textMsg
1303011586#user: textMsg


<?php

$messages = "1303011581#user: textMsg
1303011582#user: textMsg
1303011583#user: textMsg
1303011584#user: textMsg
1303011585#user: textMsg
1303011586#user: textMsg";

$messages = explode("\n", $messages);

$c_messages = count($messages);

for ($i = 0; $i < $c_messages; $i++) {
    $index = ($c_messages-$i)-1;
    if ($messages[$index] != '') {
        $message = explode("#", $messages[$index]);
        $id = $message[0];
        $text = substr($messages[1], strpos($messages[1],':')+2);
        echo "$id: $text\r";
    }
}

?>

http://codepad.org/x56Sa6qd

EDIT

After rereading your question, here is another way that should be a bit faster:

<?php

$messages = "1303011581#user: textMsg1
1303011582#user: textMsg2
1303011583#user: textMsg3
1303011584#user: textMsg4
1303011585#user: textMsg5
1303011586#user: textMsg6";

function findMessage($id, $messages) {
    if (strpos($messages, $id) === false) {
        return false;
    }
    $message = substr($messages, strpos($messages, $id));
    $message = substr($message, strpos($message, ':')+2);
    $message = substr($message, 0, strpos($message, "\n"));

    return $message;
}

echo findMessage('1303011582', $messages);

?>

http://codepad.org/i3SXT4Rn


If your question is about speed concerns, then there won't be a satisfactory answer. Reading files from the bottom is not as easy to manage. And just scanning them "in reverse" does not accomplish much.

Anyway a testworthy alternative might be to use the OS instead of PHP functions:

 $found = exec("tac file.txt | egrep '^13030115'");

But that reads through the whole file still. (Using a pipe might actually help if you have a break condition.)

A bit faster than a foreach-loop is quite usually a regex. They can split the file into lines and iterate faster than a PHP array/explode workaround does:

 $text = file_get_contents("messages.txt");
 preg_match_all('/^13030115.*?#.*?$/m', $text, $match);
 print_r($match[0]);

If you only need the last few messages, then you can make an educated guess and only read in the last 100K*$x bytes instead:

 $text = file_get_contents("messages.txt", NULL, NULL,
           max(0, filesize("messages.txt")-100000));

With a bit of heuristic logic, you could make this a loop, and backtrack the file reading if you get results from early lines.


well what you can do is put all the contents into an array:

<?php

$file = '1303011581#user: textMsg
1303011582#user: textMsg
1303011583#user: textMsg
1303011584#user: textMsg
1303011585#user: textMsg
1303011586#user: textMsg';

$fileSplode = explode("\n", $file); // pr whatever the line is split by


$info = array();
foreach($fileSplode as $line){

   list($info[], $temp) = explode('#', $line);

}

//$info contains all the first numbers

//then:
if(in_array('1303011582', $info)){
    echo 'found it!';
}?>

demo: http://codepad.org/d6PTlxtY

0

精彩评论

暂无评论...
验证码 换一张
取 消