【问题标题】:Special characters gives me "Undefined offset" error after using str_split()使用 str_split() 后,特殊字符给了我“未定义的偏移量”错误
【发布时间】:2021-10-21 12:59:50
【问题描述】:

我正在尝试做一个印刷装饰的生成器,每次调用都会产生类似“{(©+Φ+©)}”的东西。我有一些字符串,其中包含我使用 str_split() 转换为数组的装饰物每一侧的字符。我的问题是它们出现时带有臭名昭著的问号图标 �,有时我会收到错误“未定义的偏移量”。我错过了什么吗?也许使用 UTF-8 编码? --感谢任何提示!

function ornamento() {
  $izq = str_split("[{(]})");
  $der = str_split("]})[{(");
  $med = str_split("+÷×=#®©$%&?¿");
  $cen = str_split("ΔΘЖΣΤΥΦΧΨΩ");
  
  $idx1 = rand(1,sizeof($izq));
  $idx2 = rand(1,sizeof($izq));
  $mix1 = rand(1,sizeof($med));
  $mix2 = rand(1,sizeof($med));
  $cix  = rand(1,sizeof($cen));
  
  $ornamento = $izq[$idx1] . $izq[$idx2] . $med[$mix1] . $med[$mix2] .  $cen[$cix] . $med[$mix2] .  $med[$mix1] . $der[$idx2] . $der[$idx1];
  echo $ornamento;
  
}

【问题讨论】:

标签: php utf-8 character-encoding


【解决方案1】:

@Sammitch 的评论效果很好。现在的功能是:

function ornamento() {
      $izq = "[{(]})";
      $der = "]})[{(";
      $med = "+÷×=#®©$%&**";
      $cen = "ΔΘЖΣΤΥΦΧΨΩ";
      
      $idx1 = rand(1,strlen($izq));
      $idx2 = rand(1,strlen($izq));
      $mix1 = rand(1,strlen($med));
      $mix2 = rand(1,strlen($med));
      $cix  = rand(1,strlen($cen));
      
      $ornamento = mb_substr($izq,$idx1,1) . mb_substr($izq,$idx2,1) . mb_substr($med,$mix1,1) . mb_substr($med,$mix2,1) .  mb_substr($cen,$cix,1) . 
                   mb_substr($med,$mix2,1) . mb_substr($med,$mix1,1) . mb_substr($der,$idx2,1) . mb_substr($der,$idx1,1);
      return $ornamento;
      
    }

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多