0

I have two types of input string :

  1. string in a line: example:

    $str=Hello12345Hello12345Hello12345

2.string with new line ("\n\r") example:

$str = '
    Hello
    12345
    Hello
    12345
    Hello
    12345'

The ways that I use now is:

Option1:

if $str one line:
         join("\n\r",str_split($str,5))
   else:
        explode("\n\r",$str)

And Try

Option2:

preg_match_all('/[^\r\n]{0,5}([\r\n]*)/',$str , $matches);

$arr = $matches[0];

Which one should I use?

1
  • 4
    Can you describe the expected result a bit better? Commented Aug 12, 2011 at 13:15

2 Answers 2

2

Something like this?

$str = 'Hello12345Hello12345Hello12345';
$arr = str_split($string, 5);
$str = PHP_EOL . implode(PHP_EOL, $arr);
Sign up to request clarification or add additional context in comments.

Comments

1
$str1   = 'Hello12345Hello12345Hello12345';
$array1 = str_split($str1, 5);

$str2   = '
    Hello
    12345
    Hello
    12345
    Hello
    12345';
$array2 = array_filter(array_map(function($v) {
    return trim($v);
}, explode("\n", $str2)));

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.