PHP code to rearrange sentances??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dcat
    Confirmed User
    • Aug 2003
    • 1607

    #1

    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
    Too lazy to set a custom title
    • Sep 2005
    • 13826

    #2
    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:

    Comment

    • EddyTheDog
      Just Doing My Own Thing
      • Jan 2011
      • 25433

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

      Comment

      • AdultKing
        Raise Your Weapon
        • Jun 2003
        • 15601

        #4
        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

        Comment

        • Dcat
          Confirmed User
          • Aug 2003
          • 1607

          #5
          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

          • Brujah
            Beer Money Baron
            • Jan 2001
            • 22157

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

            Comment

            • Dcat
              Confirmed User
              • Aug 2003
              • 1607

              #7
              Originally posted by Brujah
              $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

              Comment

              • garce
                Confirmed User
                • Oct 2001
                • 7103

                #8
                Sentances? Try Google translate.

                Comment

                • Brujah
                  Beer Money Baron
                  • Jan 2001
                  • 22157

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

                  Comment

                  • shake
                    frc
                    • Jul 2003
                    • 4663

                    #10
                    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 credit

                    Comment

                    • glowlite
                      Confirmed User
                      • Dec 2005
                      • 366

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

                      Comment

                      • Dcat
                        Confirmed User
                        • Aug 2003
                        • 1607

                        #12
                        Originally posted by Brujah
                        $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.

                        Comment

                        • Dcat
                          Confirmed User
                          • Aug 2003
                          • 1607

                          #13
                          Originally posted by glowlite
                          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.

                          Comment

                          • sleazydoesit
                            Confirmed User
                            • May 2011
                            • 343

                            #14
                            Originally posted by glowlite
                            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

                            Comment

                            • glowlite
                              Confirmed User
                              • Dec 2005
                              • 366

                              #15
                              Originally posted by sleazydoesit
                              Don't be throwing stones if you can't use "then" and "than" correctly
                              idiot ...

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

                              Comment

                              • just a punk
                                So fuckin' bored
                                • Jun 2003
                                • 32393

                                #16
                                Originally posted by Brujah
                                $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]);
                                Obey the Cowgod

                                Comment

                                • just a punk
                                  So fuckin' bored
                                  • Jun 2003
                                  • 32393

                                  #17
                                  Or even shorter:

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

                                  Obey the Cowgod

                                  Comment

                                  • sleazydoesit
                                    Confirmed User
                                    • May 2011
                                    • 343

                                    #18
                                    Originally posted by glowlite
                                    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

                                    • Brujah
                                      Beer Money Baron
                                      • Jan 2001
                                      • 22157

                                      #19
                                      Originally posted by CyberSEO
                                      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.

                                      Comment

                                      • Brujah
                                        Beer Money Baron
                                        • Jan 2001
                                        • 22157

                                        #20
                                        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.
                                        Last edited by Brujah; 05-12-2012, 08:45 AM. Reason: fixing ucfirst

                                        Comment

                                        Working...