Jump to content

Archived

This topic is now archived and is closed to further replies.

uswebpro

quick regex help

Recommended Posts

I have a string the has either 2,3 or 4 numbers then 3 characters.
ie 88fry, 123tyd, 1256twq

How can I get the values of the seperate parts?
like:
$1 = 1256;
$2 = twq;

thanks!

Share this post


Link to post
Share on other sites
If by "characters" you mean letters from a-z.

[code]
<?php
$string = '111abc';
if (preg_match('/(^\d{2,4})([a-z]{3})$/i', $string, $matches))
{
    $numbers = $matches[1];
    $letters = $matches[2];

    print $numbers."<br />\n";
    print $letters."<br />\n";

}
?>
[/code]

Share this post


Link to post
Share on other sites
ThereĀ“s no need for regular expressions to do what you want, take a look at the [url=http://www.php.net/sscanf]sscanf()[/url] function.

ex:
[code]<?php

$string = '1256twq';
list($numbers, $letters) = sscanf($string, "%d%s");

echo "Numbers: $numbers<br />Letters: $letters";

?>[/code]

Share this post


Link to post
Share on other sites

×

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.