0

Hello

I'm want to convert string to array

$str = "[cat=[0=php,script=[js]],id=4,foo=bar]";

how to write preg pattern for change $str to :

$str = "[cat=[0=php,script=[js]]_-_id=4_-_foo=bar]";

If convert , to _-_

I can convert this string to array by foreach, but i can't convert , to _-_ in this string

I'm want convert only ,id=4,foo=bar to _-_id=4_-_foo=bar, No convert [0=php,script=[js]] to [0=php_-_script=[js]]

have any idea?

5
Contributors
5
Replies
59
Views
4 Years
Discussion Span
Last Post by iamthwee
0

how to by foreach cycle

convert any level string :

 $str = "[cat=[0=php,script=[js]],id=4,foo=bar,....]";

to :

$str = "[cat=[0=php,script=[js]]_-_id=4_-_foo=bar_-_ ...]";

have any idea?

-1

How about this:

    $str = "[cat=[0=php,script=[js]],id=4,foo=bar]";
    $arr = explode(',' , $str); 
    $arr[2] = "_-_".$arr[2];
    $arr[3] = "_-_".$arr[3];
    $str = implode('', $arr);
    print_r($str);
2

Quick 'n' dirty. Assuming that you only want to replace 'top level' commas:

$str = '[cat=[0=php,script=[js]],id=4,foo=bar,....]';
$char = str_split($str);

$level=0;
$newstring = '';
foreach($char as $s)
{
    if($s == '[')$level++;
    if($s == ']')$level--;
    if($s == ',' && $level == 1)$s = '_-_';
    $newstring .= $s;
}
echo $newstring;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.