PHP variable which can mean any value

Discussion in 'PHP' started by lordadel, Feb 14, 2008.

  1. #1
    Hello

    i am not sure if there is something like this in PHP

    what i want is a variable which can mean any variable
    as $anything=you
    and may be =playingfootball
    or even =ieieieieidd

    example:

    if (eregi('AAAA'.$anything.'BBBB', $text))
    {

    do something here;

    }

    as $anything may equal to anything (132,hello,abc123,...etc) or even equals nothing

    and the if statement returns true if the
    $text="AAAABBBB"
    or $text="AAAA123BBBB"
    or $text="AAAAhelloBBBB"
    ..etc

    i hope you understood what i mean

    i hope that someone can give me an answer

    Thanks

    Cheers
    Adel
     
    lordadel, Feb 14, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Err... I'll take a wild guess and point you to the preg_match() manual page, which I think is what you want.

    After re-reading a couple of times, give this a try:
    
    
    if (preg_match('~AAAA.*BBBB~s', $text))
    {
        // Do something
    }
    
    PHP:
     
    nico_swd, Feb 14, 2008 IP
    lordadel likes this.
  3. Ares

    Ares Member

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #3
    or you could just try this :
    
    $anything=array("you","me","someThing");
    foreach($anything as $value)
    if (eregi('AAAA'.$value.'BBBB', $text)){
    
    // do something
    
    }
    
    
    PHP:
     
    Ares, Feb 14, 2008 IP
    lordadel likes this.
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    nico_swd is the most right, but to keep you in the EREGI function, here's my 2 cents:

    if (eregi('AAAA(.*)BBBB', $text))
    {
    
    //do something here;
    
    }
    PHP:
    If you want to make sure they actually have something between the As and Bs, change the * to +

    * means zero or more
    + means one or more
    ? means one or none
    . means ANY character

    Don't worry about the parenthesis. It will not require them to have it in the string. It is placing a sub-match string inside the other string (it makes it look cleaner, to me anyways).
     
    zerxer, Feb 15, 2008 IP
    lordadel likes this.
  5. lordadel

    lordadel Active Member

    Messages:
    1,035
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    90
    #5
    thanks all for helping +rep added :)

    Cheers
     
    lordadel, Feb 15, 2008 IP