Endorphin Posted January 18, 2011 Share Posted January 18, 2011 Hi All, Im new to this forum but think its going to be a regular location for me for quite a while. Im very very new to php and the guy that was doing code for me is not responding to emails, so i thought id have a go myself...lol at the moment im going through the trial and error phase while learning. I have edited some of the code he has put on the site to create a new look. the original page: http://www.ridersguide.co.uk/county_accommodation.php?county=4; the one im trying:http://www.ridersguide.co.uk/county_accommodation2.php?county=4; The new bit is to add a preview of the text for the page. echo $row[ad_text]; the code above brings up all the text but i only want to show the first 4 or 5 lines. Is this possible in php? Any help appreciated Neil Link to comment https://forums.phpfreaks.com/topic/224883-restrict-amount-of-lines/ Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 That actually has an error in it. Associative array indices should be in quotes, otherwise they will generate an 'Undefined Constant' warning. To answer your question, yes you can use substr to limit the number of characters returned. If you define a line as 80 characters wide, 5 'lines' would be 400 characters. So if the string is over 400 characters, echo the first 400 characters, otherwise echo it in its entirety. if( strlen( $row['ad']) > 400 ) { echo substr($row['ad'], 0, 400); } else { echo $row['ad']; } EDIT: Fixed BBCode tags . . . Link to comment https://forums.phpfreaks.com/topic/224883-restrict-amount-of-lines/#findComment-1161562 Share on other sites More sharing options...
Endorphin Posted January 18, 2011 Author Share Posted January 18, 2011 Hi Pikachu2000 and thank you for such a quick response. That worked a treat!!! As I said in my initial message im very very new to this. You said that "Associative array indices should be in quotes" otherwise they can cause errors, under what circumstances would this happen as there seems to be quite a few withing the code on the page. Also, what reason would the code write it this way? Neil Link to comment https://forums.phpfreaks.com/topic/224883-restrict-amount-of-lines/#findComment-1161581 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 If the value within the [] brackets is non-numeric, it should be within 'quotes'. If it isn't, PHP is forced to check to see if there's a constant defined by that name, and if not, it then assumes that the value should be treated as a string rather than a constant. There are two main reasons why a developer wouldn't quote a string value in an array index: a) doesn't know any better, or b) is simply lazy, and figures since it usually works, why bother with the two extra keystrokes. This may or may not make any sense to you, but to illustrate a potential problem: <?php define('index', 1); // defines a constant by the name of 'index' with a value of 1 $array = array( 1 => 'First value', 'index' => 'Second value', 'string' => 'String value'); // defines an array with three indices, 1, 'index', and 'string' echo $array[index]; // Reasonably can assume that the value echoed will be 'Second value' right? Wrong. Echoes 'First value'. echo '<br>'; echo $array[string]; // Will echo the expected value since constant 'string' is not defined, but generates an 'Undefined Constant' warning and eats processing cycles echo '<br>'; echo $array[1]; // echoes correct value without warnings, and is proper. echo '<br>'; echo $array['index']; // Correct way to access value of $array['index'] echo '<br>'; echo $array['string']; // Correct. ?> Link to comment https://forums.phpfreaks.com/topic/224883-restrict-amount-of-lines/#findComment-1161593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.