Jump to content

Restrict amount of lines


Endorphin

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.