编辑:该字符串正在由浏览器输出和解释.愚蠢的错误.
在我的项目中,我创建了一个类来生成所需的HTML标记,而不是自己将它们全部回显.我在名为Card的php类中有一个名为generateTag($control,$isCardValue = true)的函数.此函数根据通过数组参数$control传递的属性生成HTML标记.该函数如下所示:
public function generateTag($control, $isCardValue = true) {
if ($isCardValue) {
// First we convert the 'class' element to an array
if (isset($control['class']) && gettype($control['class']) !== 'array') {
$control['class'] = array($control['class']);
}
// Then we add the 'card-value' class to that array.
$control['class'][] = 'card-value';
}
// The tag key is mandatory
$tag = '
// All keys other than 'tag' & 'content' are considered attributes for the HTML tag.
foreach ($control as $key => $value) {
switch ($key) {
case 'tag':
break;
case 'content':
break;
default:
if (gettype($value) === 'array') {
$tag .= ' ' . $key . '="' . implode(' ', $value) . '"';
} elseif (gettype($value) === 'NULL') {
$tag .= ' ' . $key;
} else {
$tag .= ' ' . $key . '="' . $value . '"';
}
break;
}
}
$tag .= '>';
// If the 'content' key is not passed through $control, we assume that the tag
// doesn't need to be closed (e.g. doesn't need a closing tag)
if (isset($control['content'])) {
if (gettype($control['content']) === 'array') {
foreach ($control['content'] as $child) {
$tag .= $this->generateTag($child);
}
} else {
$tag .= $control['content'];
}
$tag .= '' . $control['tag'] . '>';
}
return $tag;
}
我使用此功能来创建所有< option> < select>标签框.我只是遍历一个数组来生成标签:
foreach ($lists['tags'] as $key => $tag) {
$tag_options[$key] = array(
'tag' => 'option',
'value' => $tag['tag_id'],
'content' => $tag['tag_name_en'],
);
var_dump($card->generateTag($tag_options[$key], false));
}
这是事情变得奇怪的地方.我在生成的字符串上调用var_dump,并得到以下输出:
string(32) "" string(35) "" string(33) "" string(33) "" string(38) "" string(32) "" string(42) "" string(30) "" string(41) "" string(34) "" string(35) "" string(34) "" string(29) "" string(36) "" string(37) "" string(31) "" string(36) "" string(67) "" string(36) "" string(33) "" string(36) "" string(36) ""
似乎正在创建长度为〜35的空字符串?最奇怪的是,当我调用substr($tag_options [$key],0,1)时,它给了我
解决方法:
由于您正在浏览器中查看输出,因此它仍将每个字符串中的HTML解析为HTML,并且在呈现的页面上看不到它. var_dump不进行HTML编码.
如您所知,它可以在您页面的源代码中使用.
标签:var-dump,string,php
来源: https://codeday.me/bug/20191030/1969609.html