php wildcard carachter, matching pattern with unknown amount of whitespaces in it in php and with ereg

Posted on 2007-10-18
Last Modified: 2013-12-13
I want to detect spaces in php with ereg. (it HAS to be ereg NOT preg and NOT eregi)

I want to match a pattern that is following:
1564        XYZYZYZ      5466
ASD1 ZIA              657678767867
6AFA515 XY-Z646-55S-DUZUZ      1ADADS

The amount of dots, numbers, chars+numbers and spaces can highly vary, but template doesn't.

Therefore I am doing:
if( (ereg('([A-Z0-9-]*[A-Z0-9])( *|\n )([A-Z0-9-]*[A-Z0-9])',$val,$alt_to_be_split)) &&  (ereg('( |  |   |    |\n )',$val,$alt_str_to_replace)) )

It works but I get troubles with:
if( (ereg('([A-Z0-9-]*[A-Z0-9])( *|\n )([A-Z0-9-]*[A-Z0-9])',$val,$alt_to_be_split)) &&  (ereg('( *|\n )',$val,$alt_str_to_replace)) )

Difference between 1 and 2 is the use of * as wildcard character to get unlimited amount of whitespaces.

(like in

Any ideas of what it could be?
Question by:ChoobsTech
    LVL 2

    Author Comment

    In the same topic: Why doesn't wildcard work for dots either when doing: (\.*)
    LVL 1

    Expert Comment

    Do you want to remove whitespaces? and just use the rest splitted?
    I saw an error then in the basic regex, an extra *.
    try this one:
    ([A-Z0-9-][A-Z0-9])( *|\n )([A-Z0-9-]*[A-Z0-9])
    LVL 6

    Accepted Solution

    Maybe I just didn't understood something, bt as far as I can understand it should be like this:

    if (
        ereg('([A-Z0-9][A-Z0-9-]+)( +|\n *)([A-Z0-9][A-Z0-9-]+)', $val, $alt_to_be_split)
        ereg(' +|\n', $val, $alt_str_to_replace)
    LVL 2

    Author Comment

    The * wildcard takes the char before it an illimited amount of times.

    ereg("ap*le", "I saw the apple tree.");
    This expression matches the letter a, followed by ZERO OR MORE of the letter p, followed by the letters l and e.

    "The + meta-character provides almost the same functionality as the * character, except that the pattern must occur at LEAST ONCE"

    In my case, I prefer the + character is suitable as I need at least 1 occurence of whitespace as was pointed ou by ixti.

    To sinisazg:
    -->([A-Z0-9-]*[A-Z0-9]) works fine, and just means that [A-Z0-9-] can occur ZERO OR MORE times followed by at least 1 time [A-Z0-9].
    aces][1 WORD]

    The problem was that ( *) looks for 0 or more whitespaces, therefore is buggy when placed alone as a match criteria alone in ereg...

    To ixti:
    Don't bother to agree with me! I already changed my mind... I was going to reply with vigourous denial to your proposition at your answer before heading to bed (was coding for 17 hrs in a row).
    Few hours of sleep later, all I can say is: Why didn't I think of it!!!

    Thnx a lot!

    All points go to ixti. Feel free to make complaints in here if you feel this has been unequal.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

          Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
    Introduction (All good things must come to an end ( The original MySQL API ( has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre… provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
    Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now