3

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.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
Arnas Pečelis
  • 1,000
  • 2
  • 12
  • 32

5 Answers5

2

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
        )

)
pinkal vansia
  • 10,240
  • 5
  • 49
  • 62
  • 1
    and this is not what I need, sorry. this only removes tags, and look again what i need - move everything what is in to key and remove it from value – Arnas Pečelis Jul 24 '15 at 17:22
  • this will remove the tags but he wants the text in the tags to be used as the key for the remainder of the text in the paragraph. – Orangepill Jul 24 '15 at 17:23
1

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
        )
)
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

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

Orangepill
  • 24,500
  • 3
  • 42
  • 63
0

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!

Arnas Pečelis
  • 1,000
  • 2
  • 12
  • 32
0

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

?>
shashikant_
  • 122
  • 5