Jump to content

Regex Help


Hood15

Recommended Posts

Hello, I've already done the task by using explode and stuff. However, I think that regex would be faster and the right thing to use. Could anyone show me how this can be done with regex?

 

I'm attempting to change

 

l:blocks/nav.tpl;r:blocks/your_account.tpl,blocks/whos_online.tpl

 

in to

 

Array

(

    [l] => Array

        (

            [0] => blocks/nav.tpl

        )

 

    [r] => Array

        (

            [0] => blocks/your_account.tpl

            [1] => blocks/whos_online.tpl

        )

 

)

 

The code I used to get that with explode was

 

$_blocks = explode(';', $main_class->modules->info['blocks']);
for ($i = 0; $i < count($_blocks); $i++)
{
$side = explode(':', $_blocks[$i]);
$files = explode(',', $side[1]);
for ($file_i = 0; $file_i < count($files); $file_i++)
{
	$blocks["{$side[0]}"][] = $files["{$file_i}"];
}
}
print_r($blocks);

Link to comment
https://forums.phpfreaks.com/topic/105382-regex-help/
Share on other sites

Not a vast improvement, but regex nonetheless:

 

<pre>
<?php

$str = 'l:blocks/nav.tpl;r:blocks/your_account.tpl,blocks/whos_online.tpl';
preg_match_all('/([lr])[^;]+)/', $str, $matches);
$sides = count($matches[0]);
for ($i = 0; $i < $sides; $i++) {
	$blocks[$matches[1][$i]] = explode(',', $matches[2][$i]);
}
print_r($blocks);

?>
</pre>

Link to comment
https://forums.phpfreaks.com/topic/105382-regex-help/#findComment-539898
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.