Jump to content

Recommended Posts

Hello. I need some help filtering user input.

I want to take text that a user inputs and store it into a variable.  That's the easy part.

I have and array of 30 elements that I want to store the inputted text into.

 

So.. here's an example...

- I want a user to enter text.

- I'll store it into a variable like $inputText.

- I want to split the text from $inputText into 30 equal sections and store each section into an element in the variable.

- I don't care if the words get separated during the filter, as long as the whitespaces stay where they are.

 

Could anyone give me some tips to help me get started or point me towards a tutorial or something?

 

Thanks,

Chris

 

<?php
$userText = "Lorem ipsum dolor sito amet, atoi consectetuer adipiscing elit, sed diam nonummy nibal as eui mode. Visit autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.";

/**
* get the length and find length of the 30 portions
*/
$len = strlen($userText);
$plen = ceil($len/30);

/** 
* now go through text, plen chars at a time, and put them in the array
*/
$textArray = array();

for ($i=0; $i < $len; $i+=$plen) {
    $textArray[] = substr($userText, $i, $plen);
}

/**
* view results
*/
echo '<pre>', print_r($textArray, true), '</pre>';

?> 

Would this not be easy

 

$userText = "Lorem ipsum dolor sito amet, atoi consectetuer adipiscing elit, sed diam nonummy nibal as eui mode. Visit autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.";

 

$textArray = split(' ', $userTex);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.