简体   繁体   English

无法回显数组

[英]Can't echo an array

I have problem echo'ing an array. 我在回显数组时遇到问题。

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

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

It stops echoing at $arr[1], and I need this for a special query in mysql. 它停止在$ arr [1]处回显,我需要此来在mysql中进行特殊查询。

What I need to achieve is: 我需要实现的是:

('+one +(>two <three)

Also, I would like to know how can I use "<" character from $arr[2] to ~. 另外,我想知道如何在$ arr [2]到〜之间使用“ <”字符。 So in short, what I'm tring to achieve is: 简而言之,我想要实现的目标是:

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

It doesn't stop echoing, the browser just mistakes the < character for the start of a tag and acts accordingly. 它不会停止回显,浏览器只是将<字符误认为是标记的开头并采取相应的措施。

If you want to display this output, you should call htmlspecialchars on it first. 如果要显示此输出,则应首先对其调用htmlspecialchars Otherwise (eg for the query) no special treatment is needed. 否则(例如对于查询),不需要特殊处理。

For your last question: you 'd do something like 对于最后一个问题:您会做类似的事情

$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!

Raw output of your code: 代码的原始输出:

<br />+one +(>two <three

Browser think that <three is tag. 浏览器认为<three是标签。 Use html special chars. 使用html特殊字符。

Replace < and > with &lt; <>替换为&lt; and &gt; &gt; .

$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;

This is one way to do it. 这是做到这一点的一种方法。 There are many other ways. 还有许多其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM