Grow your CSS skills. Land your dream job.

[Solved] Adding a wild card in PHP

  • # September 27, 2010 at 5:01 pm

    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!

    # September 27, 2010 at 5:44 pm

    I’m guessing you should be looking for something like strpos().

    So the code would be

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

    } else {

    }
    # September 27, 2010 at 6:03 pm

    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 :/

    # September 27, 2010 at 6:13 pm

    Try…

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

    or look at substr_compare().

    # September 27, 2010 at 9:20 pm

    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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".