Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

hello

  1. User

    hello

  2. Assistant

    Hello! How can I assist you today?

  3. User

    Code to find a text written in a text file with php and show in which order it starts and ends

  4. Assistant

    Here's a sample code in PHP that searches for a text in a file and displays the line number, starting position, and ending position of the found text:

    $file = 'file.txt';
    $searchFor = 'Lorem ipsum';
    
    if (file_exists($file)) {
        $handle = fopen($file, "r");
        $line_num = 0;
        while(!feof($handle)) {
            ++$line_num;
            $line = fgets($handle, 4096);
            $pos = strpos($line, $searchFor);
            if ($pos !== false) {
                $startPos = $pos + 1;
                $endPos = $pos + strlen($searchFor);
                echo "Line $line_num, starting position $startPos, ending position $endPos<br>";
            }
        }
        fclose($handle);
    }
    

    Note: Replace 'file.txt' with your text file's path and 'Lorem ipsum' with your intended search term.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.