FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Requesting Help with a Regular Expression
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Requesting Help with a Regular Expression [message #179453 is a reply to message #179443] Mon, 29 October 2012 14:34 Go to previous message
bobgatski is currently offline  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
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: How to cURL a JSP page
Next Topic: Beginner's trouble with substr
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 08:20:08 GMT 2024

Total time taken to generate the page: 0.03858 seconds