Arnas Pečelis
Arnas Pečelis

Reputation: 1008

PHP move bold text to array key

I have details array:

[info_details] => Array
            (
                [0] => <b>title:</b> this is title
                [1] => <b>name:</b> this is name
                [2] => <b>created</b> this is date
            )

and I need to format this array to this:

[info_details] => Array
            (
                [title] => this is title
                [name] => this is name
                [created] => this is date
            )

so what is the best way to explode bold text? my code now:

foreach ( $array as $key => $value ) {
      $this->__tmp_data['keep'][] = preg_split('/<b[^>]*>/', $value);
}

but it doesn't work.

Upvotes: 3

Views: 1415

Answers (5)

pinkal vansia
pinkal vansia

Reputation: 10320

PHP has inbuilt function strip_tags() to strip HTML tags.

   foreach ( $array as $key => $value ) {
            $this->__tmp_data['keep'][] = strip_tags($value);
   }

UPDATE

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

?>

OUTPUT

Array
(
    [keep] => Array
        (
            [title] =>  this is title
            [name] =>  this is name
            [created] =>  this is date
        )

)

Upvotes: 2

shashikant_
shashikant_

Reputation: 122

reading above comments I guess this is what you needed.

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

?>

Upvotes: 0

MH2K9
MH2K9

Reputation: 12039

Can try this regex with preg_match() and 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>';

Output

Array
(
    [info_details] => Array
        (
            [title] => this is title
            [name] => this is name
            [created] => this is date
        )
)

Upvotes: 1

Arnas Pečelis
Arnas Pečelis

Reputation: 1008

So I found a solution, but this is hardcode...

foreach ( $array as $key => $value ) {
                $this->__tmp_data['keep'][strtolower(str_replace(':', '', strip_tags(@reset(explode('</b>', $value)))))] = trim(@end(explode('</b>', $value)));
            }

any other solutions will be accepted, even regex are welcome!

Upvotes: 0

Orangepill
Orangepill

Reputation: 24655

Assuming that the colon will always be present you can use strip_tags and explode to get what you want.

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

See it live here. Also worth noting that this implementation will remove the : from the array key and strip any html content from the trailing portion of each array element.

If you can't rely on the delimiter to be there you can instead use the close bold tag as your delimiter.

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

or run it here

Upvotes: 0

Related Questions