PHP 将粗体文本移动到数组键
PHP move bold text to array key
我有详细信息数组:
[info_details] => Array
(
[0] => <b>title:</b> this is title
[1] => <b>name:</b> this is name
[2] => <b>created</b> this is date
)
我需要将此数组格式化为:
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
那么分解粗体文本的最佳方法是什么?
我现在的代码:
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = preg_split('/<b[^>]*>/', $value);
}
但是没用。
PHP 具有内置函数 strip_tags()
以去除 HTML 标签。
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = strip_tags($value);
}
更新
<?php
$info_details = array
(
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date'
);
$tmp_data = [];
foreach ( $info_details as $key => $value ) {
list($key,$value)=explode('</b>', $value);
$tmp_data['keep'][str_replace(array(':','<b>'),'',$key)] = $value;
}
echo '<pre>';
print_r($tmp_data);
?>
输出
Array
(
[keep] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)
假设冒号始终存在,您可以使用 strip_tags 并分解以获得您想要的内容。
<?php
$info_details = array(
"<b>title:</b> this is title",
"<b>name:</b> this is name",
"<b>created:</b> this is date"
);
$return = array();
foreach($info_details as $val){
list ($key, $value) = explode(":",strip_tags($val), 2);
$return[$key] = $value;
}
print_r($return);
现场观看here。同样值得注意的是,此实现将从数组键中删除 : 并从每个数组元素的尾部删除任何 html 内容。
如果您不能依赖分隔符,您可以使用关闭的粗体标记作为分隔符。
<?php
$info_details = array(
"<b>title:</b> this is title",
"<b>name:</b> this is name",
"<b>created</b> this is date"
);
$return = array();
foreach($info_details as $val){
list ($key, $value) = explode("</b>",$val, 2);
$key = strip_tags($key);
$return[$key] = $value;
}
print_r($return);
或运行它here
所以我找到了解决方案,但这是硬编码...
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][strtolower(str_replace(':', '', strip_tags(@reset(explode('</b>', $value)))))] = trim(@end(explode('</b>', $value)));
}
任何其他解决方案都将被接受,甚至欢迎正则表达式!
可以用 preg_match()
和 str_replace()
试试这个 regex
$pattern = "/<b>.+:<\/b>\s?/";
$arr['info_details'] = [
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date',
];
$new_arr['info_details'] = [];
foreach($arr['info_details'] as $val){
preg_match($pattern, $val, $m);
$new_arr['info_details'][trim(strip_tags($m[0]), ': ')] = str_replace($m[0], '', $val);
}
print '<pre>';
print_r($new_arr);
print '</pre>';
输出
Array
(
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)
看了上面的评论,我想这就是你需要的。
<?php
$info_details = array
(
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date'
);
foreach ($info_details as $value)
{
$temp = explode("</b>",$value);
$info_details = array(strip_tags(str_replace(":","",$temp[0])) =>$temp[1]);
}
print_r($info_details);
?>
我有详细信息数组:
[info_details] => Array
(
[0] => <b>title:</b> this is title
[1] => <b>name:</b> this is name
[2] => <b>created</b> this is date
)
我需要将此数组格式化为:
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
那么分解粗体文本的最佳方法是什么? 我现在的代码:
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = preg_split('/<b[^>]*>/', $value);
}
但是没用。
PHP 具有内置函数 strip_tags()
以去除 HTML 标签。
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = strip_tags($value);
}
更新
<?php
$info_details = array
(
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date'
);
$tmp_data = [];
foreach ( $info_details as $key => $value ) {
list($key,$value)=explode('</b>', $value);
$tmp_data['keep'][str_replace(array(':','<b>'),'',$key)] = $value;
}
echo '<pre>';
print_r($tmp_data);
?>
输出
Array
(
[keep] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)
假设冒号始终存在,您可以使用 strip_tags 并分解以获得您想要的内容。
<?php
$info_details = array(
"<b>title:</b> this is title",
"<b>name:</b> this is name",
"<b>created:</b> this is date"
);
$return = array();
foreach($info_details as $val){
list ($key, $value) = explode(":",strip_tags($val), 2);
$return[$key] = $value;
}
print_r($return);
现场观看here。同样值得注意的是,此实现将从数组键中删除 : 并从每个数组元素的尾部删除任何 html 内容。
如果您不能依赖分隔符,您可以使用关闭的粗体标记作为分隔符。
<?php
$info_details = array(
"<b>title:</b> this is title",
"<b>name:</b> this is name",
"<b>created</b> this is date"
);
$return = array();
foreach($info_details as $val){
list ($key, $value) = explode("</b>",$val, 2);
$key = strip_tags($key);
$return[$key] = $value;
}
print_r($return);
或运行它here
所以我找到了解决方案,但这是硬编码...
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][strtolower(str_replace(':', '', strip_tags(@reset(explode('</b>', $value)))))] = trim(@end(explode('</b>', $value)));
}
任何其他解决方案都将被接受,甚至欢迎正则表达式!
可以用 preg_match()
和 str_replace()
$pattern = "/<b>.+:<\/b>\s?/";
$arr['info_details'] = [
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date',
];
$new_arr['info_details'] = [];
foreach($arr['info_details'] as $val){
preg_match($pattern, $val, $m);
$new_arr['info_details'][trim(strip_tags($m[0]), ': ')] = str_replace($m[0], '', $val);
}
print '<pre>';
print_r($new_arr);
print '</pre>';
输出
Array
(
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)
看了上面的评论,我想这就是你需要的。
<?php
$info_details = array
(
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date'
);
foreach ($info_details as $value)
{
$temp = explode("</b>",$value);
$info_details = array(strip_tags(str_replace(":","",$temp[0])) =>$temp[1]);
}
print_r($info_details);
?>