Tham số nhạy cảm php

/**
*
*  将一个字串中含有全角的数字字符、字母、空格或'%+-[]'字符转换为相应半角字符
* @author tekintian#gmail.com
* @param   string       $str         待转换字串
*
* @return  string       $str         处理后字串
*/
function str2byte_convert[String $str]:String  {
    $arr = array['0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
        '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
        'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
        'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
        'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
        'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
        'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
        'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
        'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
        'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
        'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
        't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
        'y' => 'y', 'z' => 'z',
        '(' => '[', ')' => ']', '〔' => '[', '〕' => ']', '【' => '[',
        '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
        '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '',
        '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
        ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
        ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
        '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
        ' ' => ' '];

    return strtr[$str, $arr];
}

In PHP 5+, objects are passed by reference. This has got me into trouble in the past when I've tried to make arrays of objects.
For example, I once wrote something like the following code, thinking that I'd get an array of distinct objects. However, this is wrong. This code will create an array of multiple references to the same object.

  class myNumber
  {
    public $value;
  }

  $arrayOfMyNumbers = array[];
  $arrayItem = new myNumber[];
  for[ $i = 0; $ivalue = $i;
    $arrayOfMyNumbers[] = $arrayItem;
  }

  var_dump[$arrayOfMyNumbers];
?>

output:
array[3] {
  [0]=>
  object[myNumber]#1 [1] {
    ["value"]=>
    int[4]
  }
  [1]=>
  object[myNumber]#1 [1] {
    ["value"]=>
    int[4]
  }
  [2]=>
  object[myNumber]#1 [1] {
    ["value"]=>
    int[4]
  }
}

notice that the value at each index in the array is the same. That is because the array is just 3 references to the same object, but we change the property of this object every time the for loop runs. This isn't very useful. Instead, I've changed the code below so that it will create an array with three distinct references to three distinct objects. [if you're having a hard time understanding references, think of this as an array of objects, and that this is just the syntax you have to use.]

  class myNumber
  {
    public $value;
  }

  $arrayOfMyNumbers = array[];
  for[ $i = 0; $ivalue = $i;
    $arrayOfMyNumbers[] = $arrayItem;
  }

  var_dump[$arrayOfMyNumbers];
?>

    return strtr[$str, $arr];
}
1

  class myNumber
  {
    public $value;
  }
0

    return strtr[$str, $arr];
}
3

    return strtr[$str, $arr];
}
4

    return strtr[$str, $arr];
}
5

    return strtr[$str, $arr];
}
6

    return strtr[$str, $arr];
}
7

    return strtr[$str, $arr];
}
8

    return strtr[$str, $arr];
}
9

In PHP 5+, objects are passed by reference. This has got me into trouble in the past when I've tried to make arrays of objects.
For example, I once wrote something like the following code, thinking that I'd get an array of distinct objects. However, this is wrong. This code will create an array of multiple references to the same object.
0

Chủ Đề