Add string to variables

Hi

Good day!

I just want to know if what syntax/code should I use to put - in my data variable.

for example I have this data.

$lot_number_indi_scan = ‘LO130315002LO130318001’;

I want to create a new variable and the output has added - between the lot_number_indi_scan like this:

$newlot = ‘LO130315002-LO130318001’;

I want to happen is first I will check the length of $lot_number_indi_scan if length is greater than 10 every lot_number_indi_scan has -
like this:
$newlot = ‘LO130315002-LO130318001’;

Any help is highly appreciated.

Thank you so much.

Well for starters your lot numbers are -11- characters long, not 10, so i’d suggest not looking for longer than 10 :wink:

[FPHP]strlen[/FPHP] will give you a means of comparing length.

If all of your items will begin with “LO”, you may find it simpler to [FPHP]explode[/FPHP] and then [FPHP]implode[/FPHP] with a hyphen…
Or [FPHP]str_replace[/FPHP] the LO’s with -LO and then [FPHP]substr[/FPHP] off the first character.

Hi,

Thank you.

I tried this code:

$chars = 11; //change to number of chars in string
$lot_number_scan = “LO130315002LO130318001,LO130315003”;
$lot_number_indi_scan = explode(‘,’, $lot_number_scan);
$newlot =‘’;
foreach ($lot_number_indi_scan as $value){
$ilen = strlen($value);

    $newlot .= substr($value, 0, $chars);
    for ($i=$chars; $i<=$ilen; $i=$i+$chars){
        $newlot .= '-' .substr($value, $i,$chars);
    }
}
$newlot = substr($newlot, 0, -1);
echo $newlot;

and the output is:

LO130315002-LO130318001-LO130315003

but I need output is:

$lot_number_indi_scan[0] = LO130315002-LO130318001
$lot_number_indi_scan[1] = LO130315003

Thank you so much.

Hi @newphpcoder,

Try this:



  $str   = "LO130315002LO130318001, LO130315003";
  echo '$str ==> ' .$str .'<br /><br />';

  $aLots = explode(',', $str);
  for($i2=0; $i2<count($aLots); $i2++):

    if( strlen( $aLots[$i2] ) > 12 ):
      $aLots[$i2] = substr($aLots[$i2], 0, 11) .'-' .substr($aLots[$i2], 11);
    endif;
    echo '$aLots[' .$i2 .'] ==> ';
    echo  $aLots[$i2];
    echo '<br />';

  endfor;

Output:

$str ==> LO130315002LO130318001, LO130315003

$aLots[0] ==> LO130315002-LO130318001
$aLots[1] ==> LO130315003

Thank you so much…It was resolved my problem.

I am pleased you took the time to say thank you and that your problem is now resolved.

I also hope you understood the script and did not just copy & paste the solution :slight_smile: