Jump to content

Need a bit help with regex


feha

Recommended Posts

Hi,

I'm collecting info of the MySQL table structure, so far im getting nice array as below:

 

What I need help with is a regex to extract values from :

[Type] => varchar(250)

and get them as :

[Type] => array('varchar' => '250') ... 

Any help appreciated, thank you in advance ...

 

Link to comment
Share on other sites

I fixed this way:

$s = 'varchar(250)';
$result = preg_split('/([0-9]+)/i', $s, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($result);

and getting:

Array ( [0] => varchar( [1] => 250 [2] => ) ) 

That's not right result ...

:-(

 

Link to comment
Share on other sites

Thanks, this is  better :-)

$s = 'varchar(250)';
$result = preg_split('#([a-z]+)\(([0-9]+)\)#i', $s, 0, PREG_SPLIT_DELIM_CAPTURE);
print_r($result);

I get:

Array ( [0] => [1] => varchar [2] => 250 [3] => ) 

btw the first 0 and last is empty ...

result should be something as :

Array ( [0] =>  varchar [1] => 250  ) 

or ?

 

Link to comment
Share on other sites

Hello again ...

Have still some problems ...

$type = 'int';
if(preg_match('/([a-z]+)\(([0-9]+)\)/i', $type, $out))
{

}
else
{
preg_match('/([a-z]+)/i', $type, $out);

}
print_r($out);

Is it possible to make it in one pass, regex ...

 

as some times values are:

int(10) unsigned,

text

date

 

not just

varchar(250)  etc ...

so it should match something like:

txt

int(10)

int(10) unsigned

but still to get matches ...

is this possible with single regex line ?

 

Thank you

f

Link to comment
Share on other sites

Hi,

I still have some problems ...

my current code is:

$pattern = "/([a-z]+)\(([0-9]+)\)([a-z]+)|([a-z]+)\(([0-9]+)\)|([a-z]+)/i";

$type = 'int        (20)         unsigned';
//$type = 'int(20)';
//$type = 'text';
$type = preg_replace('/\s+/','', $type);
if(preg_match($pattern, $type, $out))
{
echo 1;
}
echo "<pre>".print_r($out,true);

so far it works ok as OR  ...

if i try:

$type = 'int(20) unsigned';

iget

Array
(
    [0] => int(20)unsigned
    [1] => int
    [2] => 20
    [3] => unsigned
)

if i try  $type = 'int(20)';

Array
(
    [0] => int(20)
    [1] => 
    [2] => 
    [3] => 
    [4] => int
    [5] => 20
)

an if i try : $type = 'text';

i get:

Array
(
    [0] => text
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => text
)

 

it works, but the problem is i want to use just

[1],[2],[3]  without having empty values ...

is the possible just with RegEX  ...

 

Thank you in advance ...

 

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.