Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    eregi with leading *

    If i do an eregi search with a leading *, it does not wild card properly.

    Example:
    eregi("^mfc*", "mfc8000") returns true.
    eregi("^*mfc*", "Brothermfc8000") returns false, when it should be true.

    What do i do?
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You should use preg_* functions and not ereg* functions.
    The * is a quantifier not a wildcard.
    If you just want to find a mfc in a string you dson't need regex:
    PHP Code:
    if(stristr($string'mfc') === false)
    {
      print 
    'no mfc found';

    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    the string might have multiple wild cards.

    Search: '*mfc*c'
    String: 'brothermfc8000c'
    Returns true.

    How can this be acheived?
    If I'm postin here, I NEED YOUR HELP!!

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Ultragames
    How can this be acheived?
    What?
    The code I provided will find the mfc.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Right, with my first search. But if you want multiple wild cards, seperating a string such as "mfc*AN" then that wont work. It needs to check that all sub strings in the search are in the string, and that they appear in that order.
    If I'm postin here, I NEED YOUR HELP!!

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Quote Originally Posted by Ultragames
    If i do an eregi search with a leading *, it does not wild card properly.

    Example:
    eregi("^mfc*", "mfc8000") returns true.
    eregi("^*mfc*", "Brothermfc8000") returns false, when it should be true.

    What do i do?
    try [.]+mfc[.]+

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    The [.]+ did not work. I did write code that does exactly what i want, though it is quite messy.


    Note: The space was added to one, to fix an error in strpos, where a query at the begining of the string, will appear the same as if it was not found.

    Note: I know about stripos, but i was forced to use strtolower because the server does not support stripos.

    PHP Code:
    $One "BROTHERmfc1000mc";
    $Two "*mfc*mc";
    if( 
    strstr($Two'*') ){
        
    // GET POSITION OF THE WILDCARDS
        
    for($z=0$z<strlen($Two); $z++){
            
    $temp substr($Two$z1);
            if( 
    $temp == "*" ){
                
    $Cards[] = $z;
            }
        }
        
    $Cards[] = strlen($Two); // ADD AN ENDING MARKER FOR THE LOOP BELOW
        
    if( in_array(0$Cards) == true ){
            
    $WildCardBegining true;
        } else {
            
    $WildCardBegining false;
        }
        print
    "WildCard positions:<br>"print_r($Cards); print"<br>";
        
        
    // PULL OUT THE STRINGS BETWEEN THE WILDCARDS
        
    for($z=0$z<sizeof($Cards); $z++){
            if(
    $z==0){
                
    $temp substr($Two0$Cards[$z]);
                if( !empty(
    $temp) ){
                    
    $Parts[] = $temp;
                }
            } else {
                
    $temp substr($Two, ($Cards[$z-1]+1), ($Cards[$z]-($Cards[$z-1]+1)));
                if( !empty(
    $temp) ){
                    
    $Parts[] = $temp;
                }
            }
        }
        print
    "Search sections:<br>"print_r($Parts); print"<br>";
        
        print
    "Wild card at the begining: "$WildCardBegining ."<br>";
        
        
    // SEARCH FOR THE STRINGS IN THE QUERY, IN ORDER
        
    $strposOne " $One";
        for(
    $z=0$z<sizeof($Parts); $z++){
            
    $temp strpos(strtolower($strposOne), strtolower($Parts[$z]));
            if( 
    $z==){ // If searching the first part
                
    if( $WildCardBegining == true ){
                    if( 
    $temp >= ){
                        
    $PartFound++;
                    }
                }else{
                    if( 
    $temp == ){
                        
    $PartFound++;
                    }
                }
            } else {
                if( 
    $temp >= ){
                    
    $PartFound++;
                }
            }
        }
        
        
    // IF ALL STRINGS WERE FOUND, AND IN ORDER, PASS THE TEST
        
    if($PartFound == sizeof($Parts)){
            print
    "$One fits!";
        } else {
            print
    "$One is no good.";
        }

    However, if there is an easier way to get this to work, please let me know.
    Last edited by Ultragames; 08-16-2005 at 10:13 AM.
    If I'm postin here, I NEED YOUR HELP!!

  • #8
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    sorry... you dont need [] round single characters..doh! didnt think it would negatively affect it but I just tested it and .+mfc.+ works but [.]+mfc[.]+ doesnt

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    That would be becouse the . is treated as a dot (not any character) in character calsses.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •