PHP长度为32的空字符串 [英] PHP Empty string of length 32

查看:152
本文介绍了PHP长度为32的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该字符串正在由浏览器输出和解释.愚蠢的错误.

The string was being outputted and interpreted by the browser. Silly mistake.

在我的项目中,我创建了一个类来生成所需的HTML标记,而不是自己将它们全部回显.我在名为Card的php类中有一个名为generateTag($control, $isCardValue = true)的函数.此函数基于通过数组参数$control传递的属性生成HTML标记.该函数的外观如下:

In my project, I have created a class to generate the HTML tags I need, rather echo them all out myself. I have a function called generateTag($control, $isCardValue = true) in a php class called Card. This function generates an HTML tag based on the properties passed through the array parameter $control. Here is what the function looks like:

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 = '<' . $control['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. <input> 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;
}

我使用此功能为<select>框创建所有<option>标记.我只是遍历一个数组来生成标签:

I use this function to create all the <option> tags for a <select> box. I simply loop through an array to generate the tags:

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,并得到以下输出:

This is where things get weird. I call a var_dump on the generated string and I get the following output:

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)时,它应有应有的<.但是,当我呼叫substr($tag_options[$key], 0, 2)时,它给出了长度为2的空"字符串.对正在发生的事情有任何见解吗?

It appears that it's creating an empty string of length ~35? The weirdest thing is that when I call a substr($tag_options[$key], 0, 1), it gives me < as it should. But when I call substr($tag_options[$key], 0, 2), it gives me the "empty" string of length 2. Any insight on what's going on?

推荐答案

由于您正在浏览器中查看输出,因此它仍将每个字符串中的HTML解析为HTML,并且在呈现的页面上看不到它. var_dump不进行HTML编码.

Since you’re viewing the output in a browser, it still parses the HTML in each string as HTML and you don’t see it on the rendered page. var_dump doesn’t do HTML-encoding.

您发现,它可以在您页面的源代码中使用. :)

As you found out, it works in your page’s source. :)

这篇关于PHP长度为32的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆