1

I have a user input field. It will be a textarea. When user submit the form I want to check if the entered character count is more than 10. If more than 10 I want to split it. Clearly says, if I get a string

$someWords = "Pleasedon'tblowmetopiecesandthis will be a 12345 random.";

Then I want to split this string. Each parts should be maximum of 10 character long. Required result (clean text) should be something like below,

tblowmetop
iecesandth
is will be
 a 12345 r
 andom

How can I do this in PHP? I have idea about getting count using strlen($string);.

4
  • Just out of curiosity: why? Commented Oct 1, 2014 at 12:40
  • I have a column which accepts only values less than 10 character. So I need to add as small parts of 10 characters or less. Commented Oct 1, 2014 at 12:42
  • why not alter the table/column? Commented Oct 1, 2014 at 12:46
  • No. I want it like that. Commented Oct 1, 2014 at 12:55

4 Answers 4

1

You can use PHPs built-in function chunk_split():

$chunkedString = chunk_split($someWords, 10);

This gives you a string with a line break after each 10 characters.

If you like to get an array with entries of 10 characters each you can use PHPs str_split():

$chunks = str_split($someWords, 10);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This works, but it output as one string. Below answer works. Thanks for your effort
You asked for one string. But I added the use of str_split() now to get an array of 10 character long sub-strings.
1

PHP has a built-in function called str_split() That splits a string and creates an array.

$arr = str_split($someWords, 10);

This code will create an array of strings, each with a length of 10 characters. The similar function chunk_split() can be used to insert additional content every n characters.

$chunked = chunk_split($someWords, 10);

$chunked is a single string with newlines inserted every 10 characters. This function can be useful for wrapping text for output.

Comments

0
<?php

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?>

This function counts space(" ") as one character Output

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)

http://php.net/manual/en/function.str-split.php

Check this link for more information

Need to remove space use str_replace function before str_split for replace space in string

Comments

0

you could try using recursively

function submystr_to_array($str, $length, $arr_result = array() )
{
    $istr_len = strlen($str);

    if ( $istr_len == 0 )
        return $arr_result;

    $result = substr($str, 0, $length);
    $tail = substr($str, $length, $istr_len );

    if ( $result )
        array_push( $arr_result, $result );

    if( !$tail )
        return $arr_result;

    return submystr_to_array( $tail, $length, $arr_result);
}

$mystr = "Pleasedon'tblowmetopiecesandthis will be a 12345 random.";

var_dump( submystr_to_array($mystr, 10) );

result:

array(6) {
  [0]=>
  string(10) "Pleasedon'"
  [1]=>
  string(10) "tblowmetop"
  [2]=>
  string(10) "iecesandth"
  [3]=>
  string(10) "is will be"
  [4]=>
  string(10) " a 12345 r"
  [5]=>
  string(6) "andom."
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.