Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2005, 10:09 PM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pattern Matching for Sequentinal numbers like 1234

Hi,

Iam trying to match strings which contain 123456, 234567, 7890, 3456; i.e. numbers which are sequential. Number like 11234556 is not included and of course not 112339474 and 11111. So what could be the pattern matching syntax?

Cheers,
Sriram

 
Old January 20th, 2005, 03:21 PM
Authorized User
 
Join Date: Jan 2005
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There may be a better way to do this, and with programming there are millions of possibilities, but I would do this.
Code:
<?php
// $numString is your string to test against

// This assumes that you wont be looking to test strings that are 
// longer than 10 characters long. If not, your logic in your 
// loop needs to be a little more complex.

$numString = trim($numString); // Remove any whitespace

$strCheck = ""; // Initialize string to test against

for ($x = 0; $x < strlen($numString); $x++)
{
    if ($x + $numString{0} > 9)
        $strCheck .= $numString{0} + $x - 10;
    else
        $strCheck .= $numString{0} + $x;
}

if ($strCheck == $numString)
     echo "True";
else
     echo "False";
?>
If you are going to do it multiple times, you probably want to put it into a function.

Good Luck
Jeff

 
Old January 20th, 2005, 05:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a couple things to add. For increased efficiency, you should move the strlen() call to outside of the loop. The way you've written it, strlen() gets called each iteration of the loop, even though you know you're not modifying the length of the string.

Also, I don't understand what it is your loop is actually doing. What does the loop iteration variable, $x, have anything to do with finding matches in the original string?

It seems that this function can return any number of possibilities:
  1) the longest substring of incrementing numbers,
  2) the first substring of incrementing numbers,
  3) a structure or array containing ALL substrings of incrementing numbers.

In each case, you can save yourself the overhead of copying the substring into new strings by returning the start position and length of the substring.

For example:

// This function finds the first substring, if any, where at least
// two characters in succession are incrementing digits.

function find_incrementing_substring(&$str) // pass by reference
{
    $len = strlen($str)
    if ($len < 2)
    {
        return false; // can't match empty or 1-char strings
    }

    $last = (int)$str[0];
    $substr_start = 0;
    $substr_length = 1;

    for ($i = 1; $i < $len; ++$i) // start loop on 2nd char in str
    {
        if ((int)$str[$i] == ($last + 1))
        {
            ++$substr_length;
        }
        else if ($substr_length > 1) // already matched SOMEthing
        {
            break;
        }
        else // no match yet, move start position to current char.
        {
            $substr_start = $i;
        }

        $last = (int)$str[$i];
    }

    return $substr_length? array($substr_start, $substr_length) : false;
}

Do you follow the logic? Basically, we keep tabs of if, where a sequence starts, and how long it is.

If we STOP matching an existing sequence, we break from the loop and return the start/length of the found sequence.

Otherwise, we assume we haven't started matching anything yet, and advance our start pointer.


It would be very easy to modify this function to return an array of ALL such embedded sequential substrings instead of just the first one.



Take care,

Nik
http://www.bigaction.org/
 
Old January 20th, 2005, 05:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, keep in mind that I haven't tested the above code -- I just typed it all into the msg window. It's your responsibility to verify that it works, if you need it to.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find Matching pattern in Given String sanjivbshinde ASP.NET 2.0 Professional 3 October 10th, 2008 06:32 PM
Pattern Matching for Sequentinal numbers like 1234 sriram_r15 PHP How-To 0 January 19th, 2005 01:13 AM
javascript pattern matching... rbd Javascript 1 October 7th, 2004 12:20 PM
PHP Pattern Matching joanncae BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 May 17th, 2004 04:52 PM
Pattern Matching using PHP spraveens PHP Databases 2 March 23rd, 2004 10:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.