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
https://forums.phpfreaks.com/topic/199691-need-a-bit-help-with-regex/
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 ?

 

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

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

 

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.