Page 1 of 1

I need to do a particular thing with a string...

Posted: Sun Apr 18, 2010 8:03 pm
by Ratt100
Hi there, I'm currently building a site for a guy that tunes cars. I have some pages with thumbnails of the cars you click, and next to the thumbnail I have just a couple lines of it's specs (users click a link to see the full info on the car). Right now I'm in the middle of putting it all in a database, just to make it easier for me to edit or add new cars etc, but I don't want to store 2 different descriptions (long and short).

Basically I just want to cut down the full description on the thumbnails page and have it show only 6 lines. Can I make it chop the string after a certin amount of <br>'s? I have tried various functions but they usually just keep words intact or chop it to a certain character length. I need something that counts 6 <br>'s then chops it, this way the page looks neater :)

eg:

blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>
would become
blah<br>blah<br>blah<br>blah<br>blah<br>blah<br>

Hope somebody can help...

Thanks

Re: I need to do a particular thing with a string...

Posted: Sun Apr 18, 2010 8:35 pm
by omniuni
You could use this: http://www.php.net/manual/en/function.stristr.php and repeat six times. Check the first example to see how to get the front of the string instead of the end.

Re: I need to do a particular thing with a string...

Posted: Mon Apr 19, 2010 1:25 am
by Architek
if it was me doing what you are trying to do I would use explode. I use similar methods in parsing file name variables in vbs when I am either setting the delimiter format or know it will always be consistent.
http://php.net/manual/en/function.explode.php

since there is a delimiter that is consistent in your case the <br> it might look something like this.

Code: Select all

<?php

$specs = "Car: Skyline GTR<br>HP: 450<br>Boost: 31<br>Dyno Date: 01/20/2010<br>"

$arrspecs = explode("<br>", $specs);

echo $arrspecs[0];
echo $arrspecs[1];
echo $arrspecs[2];
?>
Output would look like this.

Car: Skyline GTR
HP: 450
Boost: 31

you could actually split it even further on this example to get the label and the value.

Hope that helps and I hope the syntax is close to being right as I didnt test any of it.

Re: I need to do a particular thing with a string...

Posted: Mon Apr 19, 2010 1:34 am
by Architek
crap after re-reading your posting I for some reason initially thought you were extracting the data to save in to sql... in this case you would take the array results that explode provides and then rebuild your html output which is nice since you can create it in any format such as te simple breaks or generate a list, table etc.