Thread: Adding dashes

    #1
  1. No Profile Picture
    The Dude Abides
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2000
    Location
    grass valley,ca
    Posts
    1,062
    Rep Power
    16
    Problem: Text input search query for part numbers. The part numbers in the database have dashes in them, however site visitors are typing the queries without the dash and not getting results.

    Is it possible to check for a dash and insert one if not present?

    Would substr_replace() do it?

    thanx
    chris
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Washington, USA
    Posts
    52
    Rep Power
    15
    You could use a regular expression and see if you match a - and if you don't then you can format the results using eregi_replace() or use substr(). Use strlen() to check the length also and make sure that it isn't longer than x digits so that when you add the dash at keypoints you end up with the right format. Below the code is a little lengthy, untested, and could be tightened but is one possible way to accomplish what you want. Let's assume a phone number we want in the following format:

    Desired format: 555-333-1212
    is input as 5553331212 (two dashes to be added)
    or input as 555333-1212 (dash is added between 5 and 3)
    or input as 555-333-1212 (outputs without modification)
    or input as (555)-333-1212 (produces error, too long)
    555-555-5555
    $string = "5553331212"
    if(strlen($string) > 12) { echo("too many digits in phone number"); exit; }

    if(substr($string, 4, 1) != "-") {
    $part1 = substr($string, 4, 3);
    $part1 .= "-";
    } else { $part1 = substr($string, 1, 7); }

    if(substr($string, 8, 1) != "-") {
    $part2 = "-";
    $part2 .= substr($string, 9, 4);
    } else { $part2 = substr($string, 8, 5); }

    $combined = $part1 . $part2;
    $result = substr($combined, 1, 12);

    Hope this is helpful

    ------------------
    <UL TYPE=SQUARE>
    <LI> TD Scripts
    <LI> Script School
    <LI>php-scripts
    </UL>


    [This message has been edited by TDavid (edited August 22, 2000).]

Similar Threads

  1. Problem adding time by breaking hours to seconds...
    By Rdesign in forum C Programming
    Replies: 6
    Last Post: August 31st, 2011, 10:27 AM
  2. adding to favorites
    By jsKid in forum HTML Programming
    Replies: 6
    Last Post: October 31st, 2003, 08:32 PM
  3. adding missing FROM-clause entry for table
    By kurious in forum PostgreSQL Help
    Replies: 0
    Last Post: October 23rd, 2003, 09:10 AM
  4. getting rid of dashes in a search
    By thrasher6670 in forum MySQL Help
    Replies: 6
    Last Post: December 23rd, 2002, 09:42 PM
  5. adding multiple email addr to Web Sec??
    By MissCrabTree in forum Perl Programming
    Replies: 0
    Last Post: September 11th, 2002, 02:59 PM

IMN logo majestic logo threadwatch logo seochat tools logo