0

I want PHP read my article text file like this.

sample text file :

OMG! Where is my right hand.

I try to find my right hand but I can't see it.

please tell me how to find it.

Now I have this function code

function getContent($file_path,$path=''){
        $file_path = $file_path;
        if(file_exists('./'.$file_path)){
            $f_read = fopen($path.$file_path,'r');
            $rs = "";
            while (!feof($f_read)) {
                $rs .= fread($f_read, 8192);
            }
            fclose($f_read);
        }
        else{
            echo $rs = "Not Connect File !";
        }
        return($rs);
    }

after use that code :

OMG! Where is my right hand. I try to find my right hand but I can't see it. please tell me how to find it.

I want to use PHP function read first line to string1 and after first line is string2 like this

$string1 = "OMG! Where is my right hand."

$string2 = "I try to find my right hand but I can't see it.

    please tell me how to find it."

Help me please :)

  • Answerers: please read the question. The OP wants to read the first line into $string1 and the rest of the file into $string2 - you don't seem to be addressing that 8and why did he get a downvote?) – Mawg says reinstate Monica Jan 08 '15 at 09:09

4 Answers4

0

In order to to read the first line into $string1 and the rest of the file into $string2 ...

Read the first line, as you do above, then call file_get_contents to get the rest.

Use the offset parameter to tell file_get_contents() to start reading after the end of the first line (pass the string length of the first line).

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
0

You can use below code for splitting your paragraph into string and also assign the character (like period, comma, etc.) from where do you want the paragraph to split.

preg_split('/[.?!]/',$mystring);

You may refer this link for more information: Explode a paragraph into sentences in PHP

or

http://php.net/manual/en/function.explode.php

Community
  • 1
  • 1
0

You can use $string=@file_get_contents($path_name); $string=@explode("\n",$string); //for get each line.

Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27
-1

try using explode() function of PHP

function getContent($file_path,$path=''){
            $file_path = $file_path;
            if(file_exists('./'.$file_path)){
                $f_read = fopen($path.$file_path,'r');
                $rs = "";
                while (!feof($f_read)) {
                    $rs .= fread($f_read, 8192);
                    $demo=explode('.', $rs);
                }
                fclose($f_read);
            }
            else{
                echo $rs = "Not Connect File !";
            }
            return($demo);//result wiil be stored in $demo array like $demo[0], $demo[1]
        }
Pradyumna Swain
  • 1,128
  • 1
  • 12
  • 21