Jump to content

A question in regular expression


Roee

Recommended Posts

Hi,

 

I have a row in my table (mysql) which contains something like this:

{1,1,1},{2,2,2},{3,3,3},{4,4,4}

 

How do I use reg exp to get an array of:

$arr[0] = {1,1,1}

$arr[1] = {2,2,2}

$arr[2] = {3,3,3}

$arr[3] = {4,4,4}

 

?

 

And after getting this array, How do I split it to something like:

$a[0][0] = 1

$a[0][1] = 1

$a[0][2] = 1

$a[1][0] = 2

$a[1][1] = 2

$a[1][2] = 2

$a[2][0] = 3

$a[2][1] = 3

$a[2][2] = 3

$a[3][0] = 4

$a[3][1] = 4

$a[3][2] = 4

 

Thank you very much

Roee

Link to comment
Share on other sites

$data = '{1,1,1},{2,2,2},{3,3,3},{4,4,4}';

$parts = explode('},{', $data);
$arr = array();
foreach($parts as $part)
{
    $part = str_replace(array('{','}'), '', $part);
    $bits = explode(',', $part);
    $arr[] = $bits;
}

echo '<pre>'.print_r($arr,true).'</pre>';

Link to comment
Share on other sites

You really don't like regular expressions, do you ? :)

I really think the reg ex is better for me here, beacuse all these loops waste so much time for my site which contains so many results.

So I still look for someone who'll show me how to do it with Reg Ex.

Thanks!

Link to comment
Share on other sites

I tried something like this, and its work:

preg_match_all('/{([1-9]),([1-9]),([1-9])}/', $string, $matches, PREG_SET_ORDER);  

 

but how do I change the [1-9] to something that will get 4-digit numbers and not only one digit numbers ?

 

thank you

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.