Array problem

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
digitalquake
Forum Newbie
Posts: 5
Joined: Wed Aug 20, 2003 1:52 am

Array problem

Post by digitalquake »

Hello
I have a field which have more than 2 lines of text in it. I get it from database into array. What i want to do is to show only the first 2 lines.
Any help appreciated.

Thanks
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Code: Select all

$str = substr($str, 0,
        strpos($str, "\n", 
                strpos($str, "\n")));
It returns the sub string of string $str, starting at position 0, till position
of the first newline in the string $str, starting looking for it after the first newline!
Isn't that a nice sentence???

Well it should do it at least...
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

or using regex...

Code: Select all

preg_match("/^.*\n.*\n/",$str, $matches);
echo $matches[0];
i'm honestly not sure which is faster - using regex or the strpos solution greenhorn suggested... i have a feeling it is the strpos one.
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

is there any specific character that separates you lines in db
or you picking through "\n"
?
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

yes, the escape sequence "\n" selects newline characters

find more here - http://www.php.net/manual/en/pcre.pattern.syntax.php
digitalquake
Forum Newbie
Posts: 5
Joined: Wed Aug 20, 2003 1:52 am

Post by digitalquake »

well i have problems

I normall write it to screen using echo $row["description"];
This way page shows all the products with the full length desctiption.

When i try your way, only one product's desciption is 2 lines and others are missing.

What might be the problem?

thanks
digitalquake
Forum Newbie
Posts: 5
Joined: Wed Aug 20, 2003 1:52 am

Post by digitalquake »

Ok I solved the problem guys, thanks for the your time
Post Reply