Jump to content

PHP file search, get content


brutal318

Recommended Posts

Hi, I'm a beginner, i had a problem about get content from file, i had searched but i can't solve my problem:

 

I have a text file (abc.txt).

It have content like this:

 

_*Chapter*_ : ... Author: aaa ( and then content .......vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv.......)

_*Chapter*_ : ... Author: bbb ( and then content .........vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv.........)

.....

and it loops like that.

 

I have a string (abcxyz) that need to find in contents.

If found it in content, print the name of the author (print all author have that content).

 

the _*Chapter*_ is unique, in content hasn't got that word, but the word Author maybe. But beetween the Chapter and the Author, hasn't another word Author.

 

 

 

Thanks a lot.

 

PS: I need someone can help me to code this. (don't send me a link to example or something etc...)

Link to comment
Share on other sites

This statement contradicts itself

PS: I need someone can help me to code this. (don't send me a link to example or something etc...)

First you say this:

I need someone can help me to code this.

This isn't a problem, I've got loads of material that could help you code it.  But then you say this:

don't send me a link to example or something etc...

So is what you actually mean "Can you write it for me with me doing nothing?"  >:(

Link to comment
Share on other sites

I'm sorry, but i had try so many, i can't get it.

I need an example to learn.

$sum=0;

if ($fh = fopen('/var/lib/mysql/mysql-slow....','r')) {

  while (! feof($fh)) {

if(stristr($fread($fh),"Chapter")) {

 

        if (stristr($fread($fh),"Author")) {

        $name=$fread($fh);

        if(stristr($fread($fh),"content")) {

        $sum=$sum+1;

        }

        if ($sum>=1) {echo $name;}

        }

}

 

 

this is the recently file of mine.

Link to comment
Share on other sites

Is the data in the text file delimited in anyway e.g. colon?

 

1:J.K. Rowling:"Well, I had one that I was playing Quidditch the other night," said Ron, screwing up his face in an effort to remember. "What do you think that means?".  "Probably that you're going to be eaten by a giant marshmallow or something," said Harry, turning the pages of The Dream Oracle without interest.

Link to comment
Share on other sites

<?php

$file = fopen("/home/rock/test.txt", "r");

while (!feof($file) ) {

$line = fgets($file);

$array = explode(' ', $line);

}

$count = count($array);

$j=0;

$t=0;

for ($i = 0; $i < $count; $i++) {

$boo=true;

while($boo){

$j++;

if($array[$j]="Author"){

$t=$j+1;

$name=$array[$t];}

if($array[$j]="content"){

echo $name;}

if($array[$i]="Chapter"){$boo=false;}

}

}

 

 

 

 

?>

 

 

can you fixed this?

Link to comment
Share on other sites

Hi

 

the file content:

 

Chapter jasgdjgajgsd jagsdjgasd Author class1 kjhasdy akshdhasd

lkhklhad content asdkjkflkasd

Chapter asjkdkjasdjk kjlwuroiqw Author class2 yfk23ds sadhoigfa

kjhasjdh lkhoiyr oiunv9035445

Chapter mmhjk0970973 klishhpswa Author class3 kkjhgfe content content lkhlahd

kjahsdlklaf

 

I want to print out "class1" and "class3", cause it have "content" in it's content.

Link to comment
Share on other sites

i can't edit the structure.

 

<?php

$file = fopen("/home/rock/test.txt", "r");

 

while (!feof($file) ) {

$line = fgets($file);

 

$array = explode(' ', $line);

 

$count=count($array);

 

 

for ($i = 0; $i < $count-1; $i++) {

 

 

 

$sum=0;

if($array[$i]=="Author"){$name=$array[$i+1];}

if($array[$i]=="content"){$sum++;}

if($sum>0){echo $name; echo "                      ";}

 

 

 

}

}

?>

 

i had fixed this, this can be run, print, but had a problem that if the content have two "content" then it will print two author, who can help me to print only one for author?

Link to comment
Share on other sites

Try this...

 

<?PHP
$file = "test.txt";
$lines = file($file);
$x = count($lines);
for($i=0;$i<$x;$i++) {
if(stristr($lines[$i], "content")) {
	$t1 = explode("Author", $lines[$i]);
	$chapter = $t1[0];
	array_shift($t1);
	$t2 = explode("content", $t1[0]);
	$author = "Author " . $t2[0];
	array_shift($t2);
	$content = "content " . implode("content", $t2);
	echo $chapter . "<br/>";
	echo $author . "<br/>";
	echo $content . "<br/>";
	echo "<hr>";
}
}

?>

Link to comment
Share on other sites

thanks a lot.

I have my code run.

<?php

$file = fopen("/home/rock/test1.txt", "r");

$post=array("");

while (!feof($file) ) {

$line = fgets($file);

 

$array = explode(' ', $line);

 

$count=count($array);

 

 

for ($i = 0; $i < $count-1; $i++) {

 

 

 

$sum=0;

if($array[$i]=="use"){$name=substr($array[$i+1],0,-2);}

if($array[$i]=="'help',"){$sum++;}

if($sum>0){

if(!in_array($name,$post)) {

array_push($post,$name);}

 

}

}

}

for ($i=0; $i<count($post);$i++) {echo $post[$i];echo "\n";}

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.