Jump to content


Photo

quick regex help


  • Please log in to reply
3 replies to this topic

#1 uswebpro

uswebpro
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 October 2006 - 02:27 PM

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!

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 28 October 2006 - 04:40 PM

If by "characters" you mean letters from a-z.

<?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";

}
?>


#3 uswebpro

uswebpro
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 October 2006 - 10:52 PM

awesome! thanks

#4 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 28 October 2006 - 10:57 PM

ThereĀ“s no need for regular expressions to do what you want, take a look at the sscanf() function.

ex:
<?php

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

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

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users