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
swetha
Forum Commoner
Posts: 88 Joined: Wed Sep 10, 2008 11:00 pm
Post
by swetha » Thu Oct 23, 2008 3:07 am
Code: Select all
function sorttext($str)
{
$str="First\nSecond\nThird";
explode (" ",$str);
sort($str)
}
{
$constr=sorttext($origtext);
echo $constr;
}
assume i have the above string in $str.May aim is to sort the contents in a textarea which
are on different lines.$origtext is the contents of the textarea.how do i return the sorted lines into an array?thanks for any help.
papa
Forum Regular
Posts: 958 Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm
Post
by papa » Thu Oct 23, 2008 3:15 am
Code: Select all
$origtext="First\nSecond\nThird";
function sorttext($str)
{
$str = explode("\n", $str);
sort($str);
return $str;
}
$constr = sorttext($origtext);
print_r($constr);
swetha
Forum Commoner
Posts: 88 Joined: Wed Sep 10, 2008 11:00 pm
Post
by swetha » Thu Oct 23, 2008 4:06 am
hi,
thanks for the reply.im getting the output as follows.
Array
(
[0] => aaaaaa
[1] => bbbbbb
[2] => wrwetw
[3] => zzzzzzzzz
)
But i need the output as follows :
aaaaa
bbbbb
wrwetw
zzzzzzzzz
i.e the output needs to be printed as above using echo statement and not print_r.
pls help.thanks
papa
Forum Regular
Posts: 958 Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm
Post
by papa » Thu Oct 23, 2008 4:19 am
Either skip the entire function and write: echo nl2br($origtext);
or
Instead of print_r($constr);...
Code: Select all
foreach($constr as $line) {
echo $line."<br />\n";
}
swetha
Forum Commoner
Posts: 88 Joined: Wed Sep 10, 2008 11:00 pm
Post
by swetha » Thu Oct 23, 2008 5:44 am
thanks,
this worked