acrobat
acrobat

Reputation: 57

php create array out of string one per line

i got a

$str ="sometxt<br>another<br>moretext<br>evenmoretext<br>";

i'm trying to output the string in one array per line.

kinda like this..

[0] => Array
        (['stuff'] => sometext
        )
[1] => Array
        (['stuff'] => another
        )
[2] => Array
        (['stuff'] => moretext
        )
[3] => Array
        (['stuff'] => evenmoretext
        )

i'm using regex to get the str.. if u need more clarification..i will do. thanks in advance

Upvotes: 0

Views: 492

Answers (2)

Jet
Jet

Reputation: 1325

Try this:

<?php
    $str ="sometxt<br>another<br>moretext<br>evenmoretext<br>";
    $r = array("stuff" => explode("<br>", $str));
    print_r($r);
?>

Array
(
    [stuff] => Array
        (
            [0] => sometxt
            [1] => another
            [2] => moretext
            [3] => evenmoretext
            [4] => 
        )

)

Upvotes: 1

David Conde
David Conde

Reputation: 4637

Try this:

$arr = explode("<br>", $str);

$resp = array();
foreach ( $arr as $val ){
  $resp[] = array("stuff" => $val);
}

I think this code would solve your dilemma. Tell me if you need any clarification.

Upvotes: 3

Related Questions