Page 1 of 1
Array problem
Posted: Wed Aug 20, 2003 1:52 am
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
Posted: Wed Aug 20, 2003 3:20 am
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...
Posted: Wed Aug 20, 2003 5:23 am
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.
Posted: Wed Aug 20, 2003 6:35 am
by devork
is there any specific character that separates you lines in db
or you picking through "\n"
?
Posted: Wed Aug 20, 2003 6:42 am
by will
yes, the escape sequence "\n" selects newline characters
find more here -
http://www.php.net/manual/en/pcre.pattern.syntax.php
Posted: Wed Aug 20, 2003 1:16 pm
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
Posted: Wed Aug 20, 2003 2:11 pm
by digitalquake
Ok I solved the problem guys, thanks for the your time