Jump to content

[SOLVED] Sorting sentences alphanumerically?


gamefreak13

Recommended Posts

I have hundreds of sentences (one per line) and need to sort them all alphabetically and numerically. And not just for the first letter.. I want it to sort it as far as possible.

 

Example input:

 

What was the last drink you had?
What was the last movie you watched?
What was the last 11.. just inserting a number here as an example?
What was the last book you read?

 

Example output:

 

What was the last 11.. just inserting a number here as an example?
What was the last book you read?
What was the last drink you had?
What was the last movie you watched?

 

This is my code I am using right now to read my text file and output it on my page (which is formatted as a MySQL INSERT query).

 

<?php

$file = "sentences.txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);

$output = explode("\n", $data);

foreach($output as $var) {

if($var == "") {
	echo "<br>";
} else {
	echo "INSERT INTO survey SET question='$var';<br>";
}

}

echo "Done!";

?>

Link to comment
Share on other sites

Can you be a little more specific?

 

I added this under $output = explode... and I get "Warning: Invalid argument supplied for foreach() in readtxt.php on line 17".

 

sort($output);
foreach($output as $key => $val) {
    $output = "output[" . $key . "] = " . $val . "\n";
}

Link to comment
Share on other sites

that's because inside your foreach statement you're reassigning something to $output (that's not an array)

 

this works fine:

<?php

$file = "sentences.txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);

$output = explode("\n", $data);
sort($output);
foreach($output as $var) {
   if($var == "") {
      echo "<br>";
   } else {
      echo "INSERT INTO survey SET question='$var';<br>";
   }
}
echo "Done!";

?>

 

it outputs:

INSERT INTO survey SET question='What was the last 11.. just inserting a number here as an example?';
INSERT INTO survey SET question='What was the last book you read?';
INSERT INTO survey SET question='What was the last drink you had?';
INSERT INTO survey SET question='What was the last movie you watched?';
Done!

Link to comment
Share on other sites

Thanks.. I tried adding just the sort part and it worked but I didn't realize it because it outputted a bunch of empty lines so the page appeared to be blank to me. I tried it again after reading your post and noticed the scrollbar. Doh!

 

Thanks! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.