【问题标题】:Can't echo an array无法回显数组
【发布时间】:2012-01-22 14:36:38
【问题描述】:

我在回显数组时遇到问题。

$test = 'one two three four';
$arr = explode(' ', $test);

echo '<br />+'.$arr[0].' +(>'.$arr[1].' <'.$arr[2].'';

它在 $arr[1] 处停止回显,我需要这个用于 mysql 中的特殊查询。

我需要实现的是:

('+one +(>two <three)

另外,我想知道如何使用 $arr[2] 中的“

('+one +(>two <three <four ... <infinite)

【问题讨论】:

    标签: php arrays echo


    【解决方案1】:

    它不会停止回显,浏览器只是将&lt; 字符误认为是标签的开头并采取相应的行动。

    如果你想显示这个输出,你应该先调用htmlspecialchars。否则(例如对于查询)不需要特殊处理。

    对于你的最后一个问题:你会做类似的事情

    $test = 'one two three four five'; 
    $arr = explode(' ', $test); 
    
    $out = '+'.$arr[0].' +(>'.$arr[1].' <'.implode(' <', array_slice($arr, 2)).')';
    echo htmlspecialchars($out); // only for echoing!
    

    【讨论】:

    • 谢谢乔恩。您刚刚解决了我的问题,也感谢 array_slice 提示,我从未使用过,只是学到了一些新东西。我接受了你的回答。再次感谢您。
    【解决方案2】:

    代码的原始输出:

    &lt;br /&gt;+one +(&gt;two &lt;three

    浏览器认为&lt;three 是标签。使用 html 特殊字符。

    【讨论】:

      【解决方案3】:
      $test = 'one two three four five six ...';
      $arr = explode(' ', $test);
      $total = count($arr);
      
      if ($total < 3) {
          echo 'Less than three elements found.';
          exit;
      }
      
      $tmp = '(+' . $arr[0] . ' +(>' $arr[1];
      
      for ($i = 2; $i < $total; ++$i) {
          $tmp .= ' <' . $arr[$i];
      }
      
      $tmp .= ')';
      
      echo $tmp;
      

      这是一种方法。还有很多其他方法。

      【讨论】:

        【解决方案4】:

        &amp;lt;&amp;gt; 替换为&amp;lt;&amp;gt;

        【讨论】:

          猜你喜欢
          • 2015-03-02
          • 2022-10-13
          • 2017-01-27
          • 2014-11-10
          • 2017-09-06
          • 1970-01-01
          • 2012-06-10
          • 2018-04-04
          • 1970-01-01
          相关资源
          最近更新 更多