【问题标题】:Convert PHP array into HTML tag attributes separated by spaces将 PHP 数组转换为以空格分隔的 HTML 标记属性
【发布时间】:2017-05-30 13:54:24
【问题描述】:

我需要将一个 PHP 数组转换成 HTML 标签属性,带有空格和引号,这是一个例子:

$array=array(
    'attr1'=>'value1',
    'id'=>'example',
    'name'=>'john',
    'class'=>'normal'
);

这是我需要达到的结果:

attr1="value1" id="example" name="john" class="normal"

有什么PHP函数可以做到吗?

我正在尝试这些:

  • http_build_query
  • array_walk

【问题讨论】:

标签: php html tags


【解决方案1】:

你也可以使用这个简单的一行代码,请按照下面的代码::

$array=array(
    'attr1'=>'value1',
    'id'=>'example',
    'name'=>'john',
    'class'=>'normal'
);
$data = str_replace("=", '="', http_build_query($array, null, '" ', PHP_QUERY_RFC3986)).'"';
echo $data;

输出

attr1="value1" id="example" name="john" class="normal"

【讨论】:

  • 请注意,属性为 'xmlns'=>"w3.org/2000/svg" 不能很好地发挥作用。
  • http_build_query 周围使用urldecode,因为http_build_query 会自动为URL 编码字符,否则href="mailto:email@address" 将被翻译成href="mailto%3Aemail%40address"
【解决方案2】:

使用foreach 循环来获取值和键。

$array = array(
  'attr1'=>'value1',
  'id'=>'example',
  'name'=>'john',
  'class'=>'normal');

foreach ($array as $key => $value) {
  echo $key . '="' . htmlspecialchars($value) . '" ';
}

如果你想使用一个函数,你可以自己做如下。

$array = array(
  'attr1'=>'value1',
  'id'=>'example',
  'name'=>'john',
  'class'=>'normal');

echo buildTag($array);

function buildTag ($array) {
  $tag = '';
  foreach ($array as $key => $value) {
    $tag .= $key . '="' . htmlspecialchars($value) . '" ';
  }
  return $tag;
}

【讨论】:

  • 当然假设键和值都是有效的 HTML 属性和值 - 也不需要转义(例如,值 " 中的双引号会破坏事情)跨度>
  • 是的,你完全正确,但是,我假设用户会正确使用这件作品
【解决方案3】:

我使用以下函数:

function buildAttributes($attributes)
{
    if (empty($attributes))
        return '';
    if (!is_array($attributes))
        return $attributes;

    $attributePairs = [];
    foreach ($attributes as $key => $val)
    {
        if (is_int($key))
            $attributePairs[] = $val;
        else
        {
            $val = htmlspecialchars($val, ENT_QUOTES);
            $attributePairs[] = "{$key}=\"{$val}\"";
        }
    }

    return join(' ', $attributePairs);
}

它正确地转义了特殊的 html 字符并支持布尔属性(没有值的属性)。以下输入:

[
    'name' => 'firstname',
    'value' => 'My Name',
    'required'
]

将产生:

name="firstname" value="My Name" required

【讨论】:

    【解决方案4】:

    您还可以将array_map()array_keys() 结合使用来构建您的$key=$value 字符串。

    包裹在array_filter() 中以删除空项目并最终使用implode() 将您的项目粘合在一起。

    $array = array(
        'attr1' => 'value1',
        'id'    => 'example',
        'name'  => 'john',
        'class' => 'normal',
        'c'     => null,
        'd'     => '',
        'e'     => '"abc"'
    );
    
    $attributes = implode( ' ', array_filter( array_map( function ( $key, $value ) {
        return $value ? $key . '="' . htmlspecialchars( $value ) . '"' : false;
    }, array_keys( $array ), $array ) ) );
    
    
    echo "<div " . $attributes . "></div>";
    

    结果:

    <div attr1="value1" id="example" name="john" class="normal" e="&quot;abc&quot;"></div>
    

    【讨论】:

      【解决方案5】:

      你可以使用这个功能:

      public static function arrayToStringTags( $array )
      {
          $tags = '';
      
          if(!(is_array($array) && !empty($array)))
          {
              return $tags;
          }
      
          foreach($array as $key => $value)
          {
              $tags .= $key. '="'. $value. '" ';
          }
      
          return $tags;
      }
      

      【讨论】:

      • 添加一些文字来解释答案是纯代码。
      • 说实话,大多数其他答案也没有提供任何解释......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2018-04-15
      • 2021-01-22
      • 2018-02-16
      • 1970-01-01
      • 2017-10-17
      相关资源
      最近更新 更多