tags:

views:

53

answers:

3

Hello all,

If the string is bigger then 50 chars long, I need to split it. The maximum allowed is 3 chunks of 50. It could be less then 50 but never more then 150. I don't need any special chars to be added, or to serve as "splitters"; I can break the string anywhere, no problem, since the propose is not for showing it to the user.

if (strlen($street) > 50)
{
  $streetPart1 = substr($street,0,50);
  $streetPart2 = substr($street,51,100);
  $streetPart3 = substr($street,101,150);
}

Is there a more elegant way for doing this?

UPDATE:

An example of what would arrive next:

if (strlen($street) > 50)
{
   $streetPart1 = substr($street,0,50);

   $streetPart2 = substr($street,51,100);

   $streetPart3 = substr($street,101,150);

   if(!empty($streetPart2) && empty($streetPart3) 
   {
      //add part2 only.
   }elseif(!empty($streetPart2 && !empty($streetPart3))
   {
     //add part 2 and part 3
   }
}

Thanks a lot. MEM

+1  A: 

Check the PHP's wordwrap() function.

http://php.net/manual/en/function.wordwrap.php

And check out the explode() function

http://php.net/manual/en/function.explode.php

Notinlist
Thanks. Why should we consider those instead of substr?
MEM
There is nothing wrong with `substr()`. The problem is using variables like `a1`, `a2`, `a3`, ...
Notinlist
Thanks again. So, in order to solve that what do you suggest? :) I mean, I'm absolute sure that we will not in a very long future, have more then 3. So it's not unlimited. - I will edit the question for a better understanding. I'm sure your observation it's even more pertinent there.
MEM
+1  A: 
<?
    function wrapAndCropToArray($text, $width, $lines)
    {
        $ret = array_slice(
            explode("\n",wordwrap($text,$width,"\n",true))
            , 0
            , $lines+1
        );
        if(isset($ret[$lines]))
            $ret[$lines] = "...";
        return $ret;
    }

    $test = "aadfuiosdy 34 123 412 341f2 38947 1029 384h120 39uh4 19023h 41234";
    var_dump(wrapAndCropToArray($test,10,3));
?>

Will output:

array(4) {
  [0]=>
  string(10) "aadfuiosdy"
  [1]=>
  string(10) "34 123 412"
  [2]=>
  string(5) "341f2"
  [3]=>
  string(3) "..."
}
Notinlist
So we use an array and that's the solution for not having 1, 2 3 variable names?
MEM
You can generalize your algorithm with arrays. You can `define()` some constant to 3 and then use that constant. Then you can change it easily to 2 or 4. Every data should be written only once as a rule of thumb.
Notinlist
`$parts = wrapAndCropToArray($test,CONSTANT_FOR_MAXLENGTH,CONSTANT_FOR_LINECOUNT); foreach($parts as `
Notinlist
@Notinlist you definitely are a php guru. I will do as you suggest, once I got the skills for that. I'm not yet capable of "quickly doing a function for accomplish something" for me, each line it's heavy. But I will get better. Nevertheless, thanks a lot for your time. I will mark your answer as useful, since others, better skilled then me, may found it properly accurate. Thanks again for your time.
MEM
+4  A: 

You may simply use str_split:

$parts = str_split($string, 50);

// if you want to have vars instead of array:
list($part1, $part2, $part3) = str_split($string, 50);
nikic
With the optional chunk length argument, this is very practical.
erisco
@nikic - I see... and by looking for what needs to be done next, that add part2 only or add part2 and part3, it's a proper way of doing so? (yes... it's another question... but it's so related... :) )
MEM
Since your spec limits the number of chunks to three, I find it quite clear to have three separate variables. When you need dynamic length or have many items, arrays often become the clearer solution.
erisco
@MEM: If you used the array version and both parts to add are the same you could do: `$parts = array_pad(str_split($string, 50), 3, 'THE_VALUE_TO_FILL_WITH');`
nikic