treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Adding a wild card in PHP

  • Hi Everyone!

    Quick question:

    I want to add a wild card in a script I'm writing. Here's an example:


    <?php
    $phone = Phone number;

    if ($phone == "805" . WILDCARD HERE) {
    DO SOMETHING;
    } else {
    DO SOMETHING ELSE;
    }
    ?>


    I'm very new to PHP and tried looking up a wildcard via google, but to no avail.

    Can anyone help me out?

    Thnx!
  • I'm guessing you should be looking for something like strpos().

    So the code would be

    if (strpos($phone, '805') != FALSE){

    } else {

    }


  • Umm, well...

    I want to check if someone's number starts with 805. Any number after that is irrelevant. It's the area code I'm interested in. I can't have it return false.

    Maybe something like this?

    if ($phone == "805" . *)


    this doesn't work :/
  • Try...

      if ((substr($phone, 0, 3) == "805")
    ...


    or look at substr_compare().
  • I decided to use string compare:


    <?php
    $phone = Phone number;

    if (strncmp("805", $phone, 2) == "0") {
    DO SOMETHING
    } else {
    DO SOMETHING ELSE
    }
    ?>


    This worked like a charm