dbradbury Posted January 13, 2010 Share Posted January 13, 2010 so up until a line break? Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/ Share on other sites More sharing options...
salathe Posted January 13, 2010 Share Posted January 13, 2010 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994283 Share on other sites More sharing options...
dbradbury Posted January 13, 2010 Author Share Posted January 13, 2010 how would that be done may i ask? Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994286 Share on other sites More sharing options...
mattal999 Posted January 13, 2010 Share Posted January 13, 2010 Could just do $string = "hihihi\nlolol"; echo explode("\n", $string)[0]; Untested btw, but logic seems sound. Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994292 Share on other sites More sharing options...
dbradbury Posted January 13, 2010 Author Share Posted January 13, 2010 Could just do $string = "hihihi\nlolol"; echo explode("\n", $string)[0]; Untested btw, but logic seems sound. so if this was for a variable it would be..? echo explode("\n", $row1['post_text'])[0]; something like that? Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994294 Share on other sites More sharing options...
mattal999 Posted January 13, 2010 Share Posted January 13, 2010 Correct. Just to be on the safe side, I would do: echo explode("<br />", nl2br($row1['post_text']))[0]; Because i'm not sure how well \n goes down with strings grabbed from random places. Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994295 Share on other sites More sharing options...
dbradbury Posted January 13, 2010 Author Share Posted January 13, 2010 so by logic, put this in it will only show the first line of the varible? Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994296 Share on other sites More sharing options...
salathe Posted January 13, 2010 Share Posted January 13, 2010 mattal999, function array dereferencing is not implemented in PHP (there has been talk about it in the past, the RFC is currently "declined") so you can't access the 0th array item in that matter. Also, there is no need to nl2br+explode the entire post (which may, in theory, be huge) if all that is going to be used is the first line. So, on those notes, another way (of many!) would be something like: echo strtok($row1['post_text'], "\r\n"); [ot]Using strtok a lot today.[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994300 Share on other sites More sharing options...
dbradbury Posted January 13, 2010 Author Share Posted January 13, 2010 mattal999, function array dereferencing is not implemented in PHP (there has been talk about it in the past, the RFC is currently "declined") so you can't access the 0th array item in that matter. Also, there is no need to nl2br+explode the entire post (which may, in theory, be huge) if all that is going to be used is the first line. So, on those notes, another way (of many!) would be something like: echo strtok($row1['post_text'], "\r\n"); [ot]Using strtok a lot today.[/ot] thanks that worked great!! now now if i wanted to show everything after the first line, would it consist of something similar to that? Quote Link to comment https://forums.phpfreaks.com/topic/188341-can-i-display-just-the-first-line-of-a-post/#findComment-994303 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.