Re: Requesting Help with a Regular Expression [message #179453 is a reply to message #179443] |
Mon, 29 October 2012 14:34 |
bobgatski
Messages: 11 Registered: October 2012
Karma:
|
Junior Member |
|
|
On Sunday, October 28, 2012 7:39:39 PM UTC-4, Denis McMahon wrote:
> On Sun, 28 Oct 2012 16:08:20 -0700, bobgatski wrote:
>
>
>
>> From strings such as these ...
>
>> int(11)
>
>> varchar(45)
>
>> datetime ... I am trying to extract just the type and length; i.e. for
>
>> the type I want ...
>
>> int varchar datetime ... and for the length I want ...
>
>> 11 45 NULL I can get the type using ...
>
>> (\w+)
>
>> ... but I haven't been able to get the type and length. What I think
>
>> should work is ... (\w+)(\((\d+)\))?
>
>> ... gets no matches at all,not even the type.
>
>
>
> Try:
>
>
>
> preg_match( "/(\w+)\(?(\d+)?/", $target, $arr );
>
>
>
> <?php
>
>
>
> $arr = array();
>
> $arr[] = "int(11)";
>
> $arr[] = "varchar(45)";
>
> $arr[] = "datetime";
>
>
>
> foreach ( $arr as $x ) {
>
> if ( preg_match( "/(\w+)\(?(\d+)?/", $x, $arr2 ) == 1 ) {
>
> echo "\$x = \"{$x}\"";
>
> echo ", \$arr2[0] = \"{$arr2[0]}\"";
>
> echo ", \$arr2[1] = \"{$arr2[1]}\"";
>
> if ( isset( $arr2[2] ) )
>
> echo ", \$arr2[2] = \"{$arr2[2]}\"";
>
> echo "\n";
>
> }
>
> else {
>
> echo "\$x = \"{$x}\" - No match or regex error!\n";
>
> }
>
> }
Denis, Thank you very, VERY much. I'm very new to PHP so I don't fully understand all of your code, but I've tried it and it works and I am learning the language elements I need to fully understand it. Thanks again, Bob
|
|
|