Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:22:13+00:00 2026-06-05T06:22:13+00:00

I wanted to make a very fast function to create a random paragraph .

  • 0

I wanted to make a very fast function to create a random paragraph .

So I thought , the best way would be to take an existing one , explode, shuffle, and implode .

 $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation .';

   $content = explode(" ", $content);
   shuffle ($content);
   $content = implode(" ", $content);

It works, but It produced 2 questions in my mind .

1 – why is this not working : ?

   $content = explode(" ", $content);
   $content = shuffle ($content);
   $content = implode(" ", $content);

and this does work :

   $content = explode(" ", $content);
   shuffle ($content);
   $content = implode(" ", $content);

2 – how to randomly define a number of words to drop from the array

(in order to create a random length / number of words ? )

** UPDATE I **

First of all , thanks to all the comments answers !

I ended up with this :

   $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 

 Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

   ';
   $rand_y = rand(1, 5);// rand number for the loop
   for ($b=1; $b<=$rand_y; $b++) { // repeat random times (to create paragraphes)
   $content = explode(' ', $content);
   $count = count($content) ; // word count - otherwise can return error
   $rand_x = rand(40,$count); // min 20 words , max = word count in paragraph
   $rnd = array_rand($content, $rand_x); // answer from @bsdnoobz
   $content = implode(' ', array_intersect_key($content, array_flip($rnd)));
}

1- I added the count() because without it i could get an error if the rand number is bigger than the word count.

“Second argument has to be between 1 and the number of elements in the
array”

(actually it is strange that PHP does not automtically ignore such an error – but nevermind .)

2- I did not find a simple way to add a paragraph separator (carriege return or new line) to create more than one . so i used a simple “for” loop. it turns out that adding the carriege return inside the $content$ itself will work ! (I would like to know why , i think it is being counted as part of the array! )

Anyhow – I know it is a primitive way, and not what many php gurus would choose ,- but I learned a lot , and that is the most important thing – thanks to all !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:22:14+00:00Added an answer on June 5, 2026 at 6:22 am

    shuffle() will randomize the array argument, but it returns a boolean. You can use array_rand() to pick one or more random entries from an array.

    $content = explode(' ', $content);
    $rnd = array_rand($content, 5);
    $content = implode(' ', array_intersect_key($content, array_flip($rnd)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to make an application that will take either the path of the
I wanted to make a function that populates a Grid in WPF with pictures.
I wanted to create a very restrictive security manager, so I extended SecurityManager and
I am very new to Python programming (15 minutes) I wanted to make a
I wanted to make some simple file recovery software, where I want to try
I wanted to make a basic custom keypad for the iPhone , I did
I wanted to make a post request in this website: http://www.prezup.info/index.php?page=films in order to
I wanted to make a button which is transparent until the user hovers over
I wanted to make a simple parser, for a pseudo code like language(kept rigid),
I'm using Zend FW and wanted to make a feed reader. I did the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.