[SOLVED] string manipulation question

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
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

[SOLVED] string manipulation question

Post by boo_lolly »

i'm populating a bulleted list of categories from a MySQL table. simple stuff. however, this list is going to be on the left side of a table column. like a navigation menu on the left of a website. the problem is, if the name in the database that's populating the bulleted list is too long, it extends into the rest of the website. i want to incorporate a script in my while loop, so that if the string name is over whatever characters long, go back to the 20th character, and grab the whitespace to the left of it, then skip a line.

for example, i want this...

Code: Select all

+--------------+
|  Categories  |
+--------------+
| 1. Plates    |
| 2. Spoons    |
| 3. Forks     |
| 4. Knives    |
| 5. Pressure Cookers |
| 6. Bed Sheets|
| 7. Pots and Pans |
| 8. Wine      |
| 9. Salt and Pepper |
+--------------+
to become this:

Code: Select all

+--------------+
|  Categories  |
+--------------+
| 1. Plates    |
| 2. Spoons    |
| 3. Forks     |
| 4. Knives    |
| 5. Pressure  |
|    Cookers   |
| 6. Bed Sheets|
| 7. Pots and  |
|    Pans      |
| 8. Wine      |
| 9. Salt and  |
|    Pepper    |
+--------------+
here's my code now...

Code: Select all

<UL>
<?php
        $sql = mysql_query("SELECT * FROM categories WHERE parent=0");

        while($row = mysql_fetch_array($sql)){
                echo "<LI><a href=\"index.php?cat=". $row[id] ."\">". $row[name] ."</a></LI>\n";
        }
?>
</UL>
$row['name'] is the string being manipulated. where would i start? i'm sure there's many ways to skin this cat, which method(s) should i use and why?

thanks for all your help.
Last edited by boo_lolly on Wed Jan 03, 2007 4:58 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

have a look at strlen(), str_replace().
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The <li> set the the proper CSS should do it. No need to use code. :)
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

feyd wrote:The <li> set the the proper CSS should do it. No need to use code. :)
could you be a little more specific?
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

WOuld something like this work?

Code: Select all

if (strlen($row)>19) {
	for ($n=0; $n<1; $n++) {
		preg_replace('\s', '\n\s', $row);
		}
}
I haven't tested this rubbish but maybe it will give you an idea! :D
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i looked into wordwrap() and it worked perfect for me.

Code: Select all

<UL>
<?php
        $sql = mysql_query("SELECT * FROM categories WHERE parent=0");

        while($row = mysql_fetch_array($sql)){
                echo "<LI><a href=\"index.php?cat=". $row[id] ."\">". wordwrap($row[name], 18, "<br />\n") ."</a></LI>\n";
        }
?>
</UL>
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Awesome Boo_lolly,

I didn't even know about word wrap. How did you find out about that? Web search or directly on PHP.net function list?
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

jyhm wrote:Awesome Boo_lolly,

I didn't even know about word wrap. How did you find out about that? Web search or directly on PHP.net function list?
i checked the php.net function list =)
Post Reply