'Convert PHP array into HTML tag attributes separated by spaces
I need to convert a PHP array into HTML tag attributes, with spaces and quotes, this is an example:
$array=array(
'attr1'=>'value1',
'id'=>'example',
'name'=>'john',
'class'=>'normal'
);
This is the result I need to achieve:
attr1="value1" id="example" name="john" class="normal"
There is any PHP function to do it?
I am trying these:
- http_build_query
- array_walk
Solution 1:[1]
You can also use this easy one line code, please follwo below code::
$array=array(
'attr1'=>'value1',
'id'=>'example',
'name'=>'john',
'class'=>'normal'
);
$data = str_replace("=", '="', http_build_query($array, null, '" ', PHP_QUERY_RFC3986)).'"';
echo $data;
Output
attr1="value1" id="example" name="john" class="normal"
Solution 2:[2]
Use a foreach
loop to get the value and key.
$array = array(
'attr1'=>'value1',
'id'=>'example',
'name'=>'john',
'class'=>'normal');
foreach ($array as $key => $value) {
echo $key . '="' . htmlspecialchars($value) . '" ';
}
If you wanted to use a function, you could just make your own such as the following.
$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;
}
Solution 3:[3]
I use the following function:
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);
}
It correctly escapes special html characters and supports boolean attributes (attributes without value). The following input:
[
'name' => 'firstname',
'value' => 'My Name',
'required'
]
Will produce:
name="firstname" value="My Name" required
Solution 4:[4]
You could also utilize array_map() in conjunction with array_keys() to build your $key=$value
string.
Wrapped in array_filter() to remove empty items and ultimately use implode() to glue your items together.
$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>";
Result:
<div attr1="value1" id="example" name="john" class="normal" e=""abc""></div>
Solution 5:[5]
The shortest one-line function to do that would be:
function add_attributes($attributes){
return urldecode(http_build_query(array_map(function($v){ return '"'.((string) $v).'"'; }, $attributes), '', ' '));
}
You can use it like this:
$array=array(
'attr1'=>'value1',
'id'=>'example',
'name'=>'john',
'class'=>'normal'
);
echo '<div '.add_attributes($array).'></div>';
will produce:
<div attr1="value1" id="example" name="john" class="normal"></div>
Solution 6:[6]
You can use this function:
public static function arrayToStringTags( $array )
{
$tags = '';
if(!(is_array($array) && !empty($array)))
{
return $tags;
}
foreach($array as $key => $value)
{
$tags .= $key. '="'. $value. '" ';
}
return $tags;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rana Ghosh |
Solution 2 | |
Solution 3 | Dima L. |
Solution 4 | wbq |
Solution 5 | Bastien Ho |
Solution 6 |