|
改進了一下,并把圖片的雜亂碼去掉
- //取出含數字字符串中最大的數字
- function extractMaxNumber($str) {
- $maxNumber = '';
- $isNumber = false;
- for ($i = 0; $i < strlen($str); $i++) {
- if (is_numeric($str[$i])) {
- if (!$isNumber) {
- $maxNumber = $str[$i];
- $isNumber = true;
- } else {
- $maxNumber .= $str[$i];
- }
- } else {
- $isNumber = false;
- }
- }
- return (int) $maxNumber;
- }
- //$typeoption的[rules],$choices分割成數組
- foreach ($typeoption as $keys => $types) {
- if (strpos($types[rules], 'choices') !== false) {//$typeoption找出選擇和多選的rules
- $choices[$keys][rules]=$types[rules];
- $optionids[$keys]=$types[optionid];
- $optionids=array_values($optionids);
- $choices[$keys][rules]=strstr($choices[$keys][rules],'1 = ');
- $choices[$keys][rules]=strstr($choices[$keys][rules],'"',true);//把數組截取
- for($i=1;$i<=extractMaxNumber($choices[$keys][rules]);$i++){
- $choices[$keys][rules]=str_replace("$i = ", ",$i=", $choices[$keys][rules]);
- }
- $choices[$keys][rules]=substr($choices[$keys][rules],1);//去除第一個字符
- $choices[$keys][rules]=explode(",",$choices[$keys][rules]);//字符串分割成數組
- $typeoption[$keys][rules]=$choices[$keys][rules];
- }
- }
- //$typeoptionvar,$optionids數組里的$choice分割成數組并替換
- foreach ($typeoptionvar as $key => $type){
- if (in_array($type[optionid], $optionids)){
- $choice[$key][value]=$type[value];
- for($i=1;$i<=extractMaxNumber($choice[$key][value]);$i++){
- $choice[$key][value]=preg_replace('/\s+/','', $choice[$key][value]);
- }
- $choice[$key][value]=str_split($choice[$key][value]);
- //把$choice最底層數組含數字元素替換成對應的$choices含有數字的元素并去除數字和=
- foreach($choice[$key][value] as $tion => $rule){
- foreach($choices[$key][rules] as $tions => $rules){
- if (strpos($rules, "$rule") !== false) {
- $choice[$key][value][$tion]=$choices[$key][rules][$choice[$key][value][$tion]-1];
- }
- }
- for($i=1;$i<=extractMaxNumber($choice[$key][value][$tion]);$i++){
- $choice[$key][value][$tion]=str_replace("$i=", " ", $choice[$key][value][$tion]);
- }
- }
- $choice[$key][value]=implode(" ", $choice[$key][value]);
- $typeoptionvar[$key][value]=$choice[$key][value];
- }
- }
- foreach ($typeoptionvar as $key => $type) {
- if (strpos($type[value], 'data') !== false) {
- $image[$key][value]=strstr($typeoptionvar[$key][value],'data');
- $image[$key][value]=str_replace('";}','',$image[$key][value]);
- $typeoptionvar[$key][value]=$image[$key][value];
- }
- }
復制代碼 |
|