designedfree4u Posted May 9, 2008 Share Posted May 9, 2008 How do i Echo a limited amount of text. I fopen a txt file but i only what to echo only a certain amount. Sort of like a headlines you see in news headers. <?php $file = "hello.txt"; $fp = fopen($file, "r"); while(!feof($fp)) { $data = fgets($fp, 1024); echo "$data <br>"; } fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/ Share on other sites More sharing options...
maxudaskin Posted May 9, 2008 Share Posted May 9, 2008 substr <?php $file = "hello.txt"; $fp = fopen($file, "r"); while(!feof($fp)) { $data = fgets($fp, 1024); echo substr($data,0,150); // Echos first 150 characters echo "<br />"; } fclose($fp); ?> Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536428 Share on other sites More sharing options...
designedfree4u Posted May 9, 2008 Author Share Posted May 9, 2008 Ah cool. thanks allot Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536431 Share on other sites More sharing options...
maxudaskin Posted May 9, 2008 Share Posted May 9, 2008 if you want it to cut it off at about x characters but leave the last word there you will have to use a combo of strlen/substr and preg_match/preg_match_all Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536437 Share on other sites More sharing options...
designedfree4u Posted May 9, 2008 Author Share Posted May 9, 2008 Awsome, Why is it when i fopen a word doc it prints mumbo jumbo. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536445 Share on other sites More sharing options...
maxudaskin Posted May 9, 2008 Share Posted May 9, 2008 Because the way it is written and encoded. I believe that word is just like an RTF. Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536448 Share on other sites More sharing options...
designedfree4u Posted May 9, 2008 Author Share Posted May 9, 2008 Now i'm trying to make a br every 75, which it does but it removed the 50 charactor limit What it looks like: http://www.jaybirdfolio.com/display%20image.php <?php $file = "hello.txt"; $fp = fopen($file, "r"); while(!feof($fp)) { $data = fgets($fp, 1024); $text = $data; $newtext = wordwrap($data, 75, "<br />\n"); echo $newtext; echo substr($newtext,0,50); // Echos first 50 characters } fclose($fp); ?> Eventualy i'm going to try to place into a div. Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536475 Share on other sites More sharing options...
designedfree4u Posted May 9, 2008 Author Share Posted May 9, 2008 never mind i think i'm going to format with css Link to comment https://forums.phpfreaks.com/topic/104789-how-to-echo-a-limited-number-of-text/#findComment-536486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.