brutal318 Posted March 8, 2011 Share Posted March 8, 2011 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...) Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/ Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 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?" Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184413 Share on other sites More sharing options...
systemick Posted March 8, 2011 Share Posted March 8, 2011 HuggieBear is right. You need to do some work for yourself. I suggest you start with the PHP functions strpos and strstr Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184429 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184451 Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184456 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 the data file is only text. So what should i do? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184458 Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 OK, what separates the chapter from the author and the author from the content? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184475 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 it's only seperate and enter beetween line. Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184509 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184512 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 Hi, anyone can help me? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184555 Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 If you have no standard data structure within your text file it's going to be difficult I think. Can you edit the structure of the original text file? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184556 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184563 Share on other sites More sharing options...
brutal318 Posted March 8, 2011 Author Share Posted March 8, 2011 anyone else help me? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184593 Share on other sites More sharing options...
HuggieBear Posted March 8, 2011 Share Posted March 8, 2011 Can you edit the structure of the original text file? Is there an answer to this? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184649 Share on other sites More sharing options...
brutal318 Posted March 9, 2011 Author Share Posted March 9, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184829 Share on other sites More sharing options...
litebearer Posted March 9, 2011 Share Posted March 9, 2011 In the text file, other than the first 'Chapter', is each 'Chapter' preceded by a newline character? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184844 Share on other sites More sharing options...
litebearer Posted March 9, 2011 Share Posted March 9, 2011 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184862 Share on other sites More sharing options...
brutal318 Posted March 9, 2011 Author Share Posted March 9, 2011 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";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184884 Share on other sites More sharing options...
litebearer Posted March 9, 2011 Share Posted March 9, 2011 Ummm, is this a new question? is the first one solved? Quote Link to comment https://forums.phpfreaks.com/topic/229969-php-file-search-get-content/#findComment-1184942 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.