I've been handling this problem for too long and i am not close to finding any solution
My current problem is that: A line should not exceed more than 18 Length. Alphabet Letters - are considered 2 length. Special Characters such as '&', '(space)' , '(',')','[',']' are considered 1 length. Special characters are only limited to those.
The special case is that if the word length is more than 9 and it has a special character such as '&' it would create a newline and it would look like this The letters are stored in an array called
products = {'any&letter','any&body'};
Example : any&letter :
any = 6
&letter = 15 //new line
Example : any&body : any&body = 15 no new line
here is my current unfinished code but what i observed is that it has too many if statements and foreach that it may impact performance. Is there anyway i can reduce the number of foreach and if in my code?
$uniqueChar = [' ', '(', '(', '&', '【', '】', ')', ')'];
foreach ($products as $key => $numProduct) {
$word = $numProduct['MstProduct']['name'];
$storeString = [
'StoredString' => '',
'MaxLength' => 0,
]; // store the newline position here
$collectionPositionParenthesisOpening = [];
$collectionPositionBracketOpening = [];
$collectionPositionParenthesisClosing = [];
$collectionPositionbracketClosing = [];
$tempBracketClosing = [];
$tempParenthesisClosing = [];
$storeString[0] = '';
$reverse = [];
$lineNumber = 0;
$line_number = 0;
$maxLength = 0;
$parenthesis = false;
$NoSpecial = false;
$parenthesisPosition = 0;
$maxLength = 0;
$startingPosition = 0;
$stringLength = mb_strlen($word, $encoding);
$collectionWord = preg_split('//u', $word, null, PREG_SPLIT_NO_EMPTY); // convert to array
if ($stringLength > 9) {
foreach ($collectionWord as $position => $characters) {
if(in_array($characters, $uniqueChar) == true){
$maxLength = $maxLength + .5;
$prevWord = mb_substr($word, $startingPosition, $position-$startingPosition+1);
$collection[$line_number] = $prevWord;
if ($characters == '(' || $characters == '(') {
array_push($collectionPositionParenthesisOpening, ['position' => $position , 'maxLength' => $maxLength]);
} elseif ($characters == ')' || $characters == ')') {
array_push($collectionPositionParenthesisClosing, ['position' => $position , 'maxLength' => $maxLength]);
} elseif ($characters == '【') {
array_push($collectionPositionBracketOpening, ['position' => $position , 'maxLength' => $maxLength]);
} elseif ($characters == '】') {
array_push($collectionPositionbracketClosing, ['position' => $position , 'maxLength' => $maxLength]);
}
$line_number++;
$startingPosition = $position + 1;
$maxLength = 0;
$NoSpecial = False;
}
if (is_numeric($characters)) {
$maxLength = $maxLength + .5;
}
if (mb_strwidth($characters) == 2 && $characters != ')' && $characters != '(' && $characters != '【' && $characters != '】') { //fullwidth katakana
$maxLength++; //add 1
}
if (($maxLength % 9) == 0 && $maxLength >= 1 && $NoSpecial == false) {
$prevWord = mb_substr($word, $startingPosition, $position-$startingPosition);
$NoSpecial = True;
$startingPosition = $position;
$collection[$line_number] = $prevWord;
$line_number++;
}
}
$line_number = 0;
foreach ($collection as $collectionKey => $strings) {
$specialChar = mb_substr($strings, mb_strlen($strings) - 1); // get the special character at the end
if ($specialChar == '&' ) {
$prevWord = mb_substr($strings,0 ,mb_strlen($strings) - 1);
if(mb_strlen($prevWord . $storeString[$lineNumber]) < 18){
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
} else {
$lineNumber++;
$storeString[$lineNumber] = '';
array_push($storeString, "\r\n");
$lineNumber++;
$storeString[$lineNumber] = '';
$prevWord = mb_substr($strings,0 ,mb_strlen($strings)-1);
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
}
}elseif ($specialChar == '(' || $specialChar == '(') { //not inside square bracket
$reverse = array_reverse($collectionPositionParenthesisClosing); //get its pair
array_push($tempParenthesisClosing, $reverse[0]); // put in temp array
$prevWord = mb_substr($strings,0 ,mb_strlen($strings) - 1); // before special character letters
if($collectionPositionParenthesisOpening[0]['maxLength'] < 9){ //equal to 0 because it could be the first character in the product name
if(!empty($storeString[$lineNumber])) {
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
} else {
$storeString[$lineNumber] = $prevWord;
}
}
if(mb_strlen($storeString[$lineNumber]) == 0) {
$storeString[$lineNumber] = $storeString[$lineNumber] . $specialChar;
}
if(($reverse[0]['maxLength'] - $collectionPositionParenthesisOpening[0]['maxLength']) + mb_strlen($storeString[$lineNumber]) > 9){
$lineNumber++;
array_push($storeString, "\r\n"); // add whitespace
$lineNumber++;
array_push($storeString, $specialChar); // add character
}
$storeString['MaxLength'] = $storeString['MaxLength'] + $collectionPositionParenthesisOpening[0]['maxLength'];
unset($collectionPositionParenthesisOpening[0]); // remove the opening parenthesis from the collection
unset($collectionPositionParenthesisClosing[sizeof($collectionPositionParenthesisClosing) - 1]); // remove the closing parenthesis from the selection
} elseif ($specialChar == '【') { // not inside parenthesis
$reverse = array_reverse($collectionPositionbracketClosing);
array_push($tempBracketClosing, $reverse[0]); // put in temp array
$prevWord = mb_substr($strings,0 ,mb_strlen($strings) - 1); // before special character letters
if ($collectionPositionBracketOpening[0]['maxLength'] < 9) { //equal to 0 because it could be the first character in the product name
if(!empty($storeString[$lineNumber])) {
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
} else {
$storeString[$lineNumber] = $prevWord;
}
}
if (mb_strlen($storeString[$lineNumber]) == 0) {
$storeString[$lineNumber] = $storeString[$lineNumber] . $specialChar;
}
if(($reverse[0]['maxLength'] - $collectionPositionBracketOpening[0]['maxLength']) + mb_strlen($storeString[$lineNumber]) > 9){
$lineNumber++;
$storeString[$lineNumber] = '';
$storeString[$lineNumber] = "\r\n"; // add whitespace
$lineNumber++;
$storeString[$lineNumber] = '';
$storeString[$lineNumber] = $specialChar;
}
unset($collectionPositionBracketOpening[0]); // remove the opening parenthesis from the collection
unset($collectionPositionbracketClosing[sizeof($collectionPositionbracketClosing) - 1]); // remove the closing parenthesis from the selection
} elseif ($specialChar == ')' || $specialChar == ')') {
$prevWord = mb_substr($strings,0 ,mb_strlen($strings));
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
$storeString['MaxLength'] = $storeString['MaxLength'] + $tempParenthesisClosing[0]['maxLength'];
} elseif ($specialChar == '】') {
if (isset($tempBracketClosing[0]) && $storeString['MaxLength'] + $tempBracketClosing[0]['maxLength'] > 9) {
$lineNumber++;
$storeString[$lineNumber] = '';
$storeString[$lineNumber] = "\r\n"; // add whitespace
$lineNumber++;
$storeString[$lineNumber] = '';
$prevWord = mb_substr($strings,0 ,mb_strlen($strings));
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
$storeString['MaxLength'] = $storeString['MaxLength'] + $tempBracketClosing[0]['maxLength'];
} else {
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
}
unset($tempBracketClosing[0]);
} elseif ($specialChar == " ") {// not inside parenthesis or square bracket
$prevWord = mb_substr($strings,0 ,mb_strlen($strings) - 1);
if(mb_strwidth($prevWord . $storeString[$lineNumber]) < 18){
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
} else {
$lineNumber++;
$storeString[$lineNumber] = '';
$storeString[$lineNumber] = "\r\n";
$lineNumber++;
$storeString[$lineNumber] = '';
$prevWord = mb_substr($strings,0 ,mb_strlen($strings));
$storeString[$lineNumber] = $prevWord;
}
} else {
$storeString[$lineNumber] = $storeString[$lineNumber] . $strings;
$storeString['MaxLength'] = 9;
}
}
if ($startingPosition < $stringLength) {
$prevWord = mb_substr($word,$startingPosition);
if(mb_strwidth($storeString[$lineNumber] . $prevWord) < 18){
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
} else {
$lineNumber++;
$storeString[$lineNumber] = '';
$storeString[$lineNumber] = "\r\n";
$lineNumber++;
$storeString[$lineNumber] = '';
$storeString[$lineNumber] = $storeString[$lineNumber] . $prevWord;
}
}
unset($storeString['StoredString']);
unset($storeString['MaxLength']);
$products[$key]['MstProduct']['name'] = implode($storeString); // return to string form
}
}
Aucun commentaire:
Enregistrer un commentaire