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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ratt100
Forum Newbie
Posts: 1
Joined: Sun Apr 18, 2010 7:54 pm

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

Post 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
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

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

Post 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.
User avatar
Architek
Forum Commoner
Posts: 44
Joined: Wed Jul 01, 2009 5:01 am
Location: Portland OR

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

Post 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.
User avatar
Architek
Forum Commoner
Posts: 44
Joined: Wed Jul 01, 2009 5:01 am
Location: Portland OR

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

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