doutang7383 2013-07-12 14:38
浏览 33
已采纳

PHP空字符串长度为32

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

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

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));
}

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) ""

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?

  • 写回答

1条回答 默认 最新

  • doujiang1001 2013-07-12 15:17
    关注

    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. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退