GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP code to rearrange sentances?? (https://gfy.com/showthread.php?t=1067851)

Dcat 05-11-2012 05:59 PM

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?

HomerSimpson 05-11-2012 06:21 PM

shuffle()


EddyTheDog 05-11-2012 06:22 PM

You could do it with any spinner type setup....

AdultKing 05-11-2012 06:23 PM

If you want to shuffle the same every time then use this function

Code:

<?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]);
  }
}
?>

*Taken from php.net

Dcat 05-11-2012 08:23 PM

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".

Brujah 05-11-2012 08:35 PM

$sentences = explode('.', $string);
shuffle($sentences);
echo implode(' ', $sentences);

Dcat 05-11-2012 08:44 PM

Quote:

Originally Posted by Brujah (Post 18942740)
$sentences = explode('.', $string);
shuffle($sentences);
echo implode(' ', $sentences);

Something like that, but that would also consider all the potential variations such as ! !! !!! ? ." word.word(<-no space) ..etc

garce 05-11-2012 08:56 PM

Sentances? Try Google translate.

Brujah 05-11-2012 09:19 PM

$sentences = preg_split( '/\.|\!+/', $s);
array_map( 'trim', $sentences);
echo implode( '. ', $sentences );

shake 05-11-2012 09:28 PM

Basically you'll want to load them into an array and mix them up. Naturally they won't make a lot of sense afterwords.

glowlite 05-11-2012 10:09 PM

Sorry, if you can't spell than PHP is above your pay grade.

Dcat 05-11-2012 10:24 PM

Quote:

Originally Posted by Brujah (Post 18942777)
$sentences = preg_split( '/\.|\!+/', $s);
array_map( 'trim', $sentences);
echo implode( '. ', $sentences );

Thanks.

I might have to go ahead and roll my own using something similar.

Dcat 05-11-2012 10:37 PM

Quote:

Originally Posted by glowlite (Post 18942813)
Sorry, if you can't spell than PHP is above your pay grade.

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.

sleazydoesit 05-11-2012 10:59 PM

Quote:

Originally Posted by glowlite (Post 18942813)
Sorry, if you can't spell than PHP is above your pay grade.

Don't be throwing stones if you can't use "then" and "than" correctly :2 cents:

glowlite 05-11-2012 11:10 PM

Quote:

Originally Posted by sleazydoesit (Post 18942846)
Don't be throwing stones if you can't use "then" and "than" correctly :2 cents:

idiot ...

http://www.wikihow.com/Use-Than-and-Then

just a punk 05-12-2012 01:25 AM

Quote:

Originally Posted by Brujah (Post 18942777)
$sentences = preg_split( '/\.|\!+/', $s);
array_map( 'trim', $sentences);
echo implode( '. ', $sentences );

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]);

just a punk 05-12-2012 01:30 AM

Or even shorter:

preg_match_all('/.*?[\?|\.\.\.|\.|\!]+/s', $s, $sentences);
shuffle($sentences[0]);
echo implode(' ', array_map('trim', $sentences[0]));

:)

sleazydoesit 05-12-2012 01:44 AM

Quote:

Originally Posted by glowlite (Post 18942853)

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.

Brujah 05-12-2012 08:31 AM

Quote:

Originally Posted by CyberSEO (Post 18942940)
Or even shorter:

preg_match_all('/.*?[\?|\.\.\.|\.|\!]+/s', $s, $sentences);
shuffle($sentences[0]);
echo implode(' ', array_map('trim', $sentences[0]));

:)

I was drunk coding! I'll take a look and give it a try.

Brujah 05-12-2012 09:31 AM

I like this version so far:

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 );

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.

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