Anyone know of a php function, or snippet of code I can use to rearrange/randomize the order of sentences in a block of text?
PHP code to rearrange sentances??
Collapse
X
-
shuffle()
Make a bank with Chaturbate - the best selling webcam program
Ads that can't be block with AdBlockers !!! /// Best paying popup program (Bitcoin payouts) !!!
PHP, MySql, Smarty, CodeIgniter, Laravel, WordPress, NATS... fixing stuff, server migrations & optimizations... My ICQ: 27429884 | Email:
-
-
If you want to shuffle the same every time then use this function
*Taken from php.netCode:<?php /* $seed variable is optional */ function SEOshuffle(&$items, $seed=false) { $original = md5(serialize($items)); mt_srand(crc32(($seed) ? $seed : $items[0])); for ($i = count($items) - 1; $i > 0; $i--){ $j = @mt_rand(0, $i); list($items[$i], $items[$j]) = array($items[$j], $items[$i]); } if ($original == md5(serialize($items))) { list($items[count($items) - 1], $items[0]) = array($items[0], $items[count($items) - 1]); } } ?>Comment
-
Here's what I'm looking for..
Example:
$string = "Lorem ipsum is the most common form of "Greeking". However more and more people are sick and tired of using the same sample text over and over again. Also lorem ipsum is in latin and it may not always be the best choice. We tried to have text generated in some of the most widely used languages but if you are in desperate need of random text in your own language, send us an email and we'll add it here. ";
$newstring = awesomeSentenceShufflingFunction($string);
echo $newstring;
-----
Output:
We tried to have text generated in some of the most widely used languages but if you are in desperate need of random text in your own language, send us an email and we'll add it here. However more and more people are sick and tired of using the same sample text over and over again. Also lorem ipsum is in latin and it may not always be the best choice. Lorem ipsum is the most common form of "Greeking".Last edited by Dcat; 05-11-2012, 07:34 PM.Comment
-
$sentences = explode('.', $string);
shuffle($sentences);
echo implode(' ', $sentences);- Free Premium Domain Lists and Tools at Clickmojo.com
- For Sale: Obscenity.com
Comment
-
Comment
-
$sentences = preg_split( '/\.|\!+/', $s);
array_map( 'trim', $sentences);
echo implode( '. ', $sentences );- Free Premium Domain Lists and Tools at Clickmojo.com
- For Sale: Obscenity.com
Comment
-
Basically you'll want to load them into an array and mix them up. Naturally they won't make a lot of sense afterwords.Crazy fast VPS for $10 a month. Try with $20 free creditComment
-
Yeah, ok smart ass. "sentences" ....I'm DOG fucking tired today.
Did you read what I wrote in the same post?
"Anyone know of a php function, or snippet of code I can use to rearrange/randomize the order of sentences in a block of text?"
..seems "sentences" was spelled correctly.
Did you notice my example.. $newstring = awesomeSentenceShufflingFunction($string);
...Sentences is spelled correctly there too.
..give me a fucking break.
Anyways, I need sleep. I'm out.
Thanks to all of those with something constructive to offer. I'll check back tomorrow.Comment
-
Comment
-
-
The code above won't work because:
1) it doesn't actually shuffle sentences;
2) it doesn't split sentences that end with "..." and "?";
3) the result of array_map() will be lost;
4) all exclamation marks will be replaced with dots.
So here is the fixed version:
preg_match_all('/.*?[\?|\.\.\.|\.|\!]+/s', $s, $sentences);
$sentences[0] = array_map('trim', $sentences[0]);
shuffle($sentences[0]);
echo implode(' ', $sentences[0]);Obey the CowgodComment
-
Or even shorter:
preg_match_all('/.*?[\?|\.\.\.|\.|\!]+/s', $s, $sentences);
shuffle($sentences[0]);
echo implode(' ', array_map('trim', $sentences[0]));
Obey the CowgodComment
-
I repeat, don't be throwing stones if you can't use "then" and "than" correctly, and if you rely on a fucking website called Wikihow for grammar lessons then you have some serious problems.Comment
-
-
I like this version so far:
This kept the mix of punctuation and any number of it together, and in case of a fragment with lowercase first letter it'll fix that.Code:$string = <<<EOS Lorem ipsum is the most common form of "Greeking". However more and more people are sick and tired of using the same sample text over and over again. Also lorem ipsum is in latin and it may not always be the best choice. We tried to have text generated in some of the most widely used languages but if you are in desperate need of random text in your own language, send us an email and we'll add it here. Adding a few extra examples... you mean like this? Hmm, what the hell?!!! EOS; preg_match_all( '/[^.?!]+[.?!]+/m', $string, $matches ); $sentences = array_map( 'trim', $matches[0] ); shuffle( $sentences ); $sentences = array_map( 'ucfirst', $sentences ); echo implode( ' ', $sentences );
Outputs:
However more and more people are sick and tired of using the same sample text over and over again. We tried to have text generated in some of the most widely used languages but if you are in desperate need of random text in your own language, send us an email and we'll add it here. You mean like this? Adding a few extra examples... Hmm, what the hell?!!! Lorem ipsum is the most common form of "Greeking". Also lorem ipsum is in latin and it may not always be the best choice.- Free Premium Domain Lists and Tools at Clickmojo.com
- For Sale: Obscenity.com
Comment

Comment