55573

How to split PHP string if character count is greater

Question:

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

Answer1:

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

<a href="http://php.net/manual/en/function.str-split.php" rel="nofollow">http://php.net/manual/en/function.str-split.php</a>

Check this link for more information

Need to remove space use <strong>str_replace</strong> function before <strong>str_split</strong> for replace space in string

Answer2:

You can use PHPs built-in function <a href="http://php.net/manual/en/function.chunk-split.php" rel="nofollow">chunk_split()</a>:

$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 <a href="http://php.net/manual/en/function.str-split.php" rel="nofollow">str_split()</a>:

$chunks = str_split($someWords, 10);

Answer3:

PHP has a built-in function called <a href="http://php.net/manual/en/function.str-split.php" rel="nofollow">str_split()</a> 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 <a href="http://php.net/manual/en/function.chunk-split.php" rel="nofollow">chunk_split()</a> can be used to insert additional content every n characters.

$chunked = chunk_split($someWords, 10);

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

Answer4:

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

Recommend

  • Replace first character with last character of multiple strings PHP
  • PHP find string value and marked it by position in array
  • compare values of an array, with the key of an array of objects
  • PHP COUNT_RECURSIVE only count non-array values?
  • Pairwise subtraction of two arrays
  • PHP - Check if a string is a rotation of another string
  • How to Add two array values in php? [duplicate]
  • Using PHP write an anagram function?
  • Merging 2 arrays with different value types
  • How to loop through multiple arrays to find something common
  • How to sort an array considering localization?
  • Store array in cookie
  • Cookie multiple values
  • Javascript, map returns undefined
  • Facebook Graph API: Permissions to Friends Photos
  • Recursive regex not matching template blocks
  • friend declaration in protected section
  • Peer to peer/adaptive payments with paypal [closed]
  • How to Divide an array on c#?
  • What Makes These Two Array Adds Different?
  • Parse a date string in a specific locale (not timezone!)
  • Install PHP intl extension on MacOS
  • Retrieve list of sent friend requests from friend_request FQL table
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Bad request using file_get_contents for PUT request in PHP
  • Refering to the class itself from within a class mehod in Objective C
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • How to handle AllServersUnavailable Exception
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • 0x202A in filename: Why?
  • A cron job substitute?
  • json Serialization in asp
  • Error creating VM instance in Google Compute Engine
  • Python/Django TangoWithDjango Models and Databases