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 #179443 is a reply to message #179442] Sun, 28 October 2012 23:39 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
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";
}
}
[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: Thu Nov 28 03:52:11 GMT 2024

Total time taken to generate the page: 0.04230 seconds