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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.