![]() |
PHP code to rearrange sentances??
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?
|
shuffle()
|
You could do it with any spinner type setup....
|
If you want to shuffle the same every time then use this function
Code:
<?php |
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". |
$sentences = explode('.', $string);
shuffle($sentences); echo implode(' ', $sentences); |
Quote:
|
Sentances? Try Google translate.
|
$sentences = preg_split( '/\.|\!+/', $s);
array_map( 'trim', $sentences); echo implode( '. ', $sentences ); |
Basically you'll want to load them into an array and mix them up. Naturally they won't make a lot of sense afterwords.
|
Sorry, if you can't spell than PHP is above your pay grade.
|
Quote:
I might have to go ahead and roll my own using something similar. |
Quote:
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. |
Quote:
|
Quote:
http://www.wikihow.com/Use-Than-and-Then |
Quote:
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]); |
Or even shorter:
preg_match_all('/.*?[\?|\.\.\.|\.|\!]+/s', $s, $sentences); shuffle($sentences[0]); echo implode(' ', array_map('trim', $sentences[0])); :) |
Quote:
|
Quote:
|
I like this version so far:
Code:
$string = <<<EOS 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. |
All times are GMT -7. The time now is 03:22 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123