SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get first part of variable

    I would like to extract "123" from the variable "123 anywhere street"

    Any ideas?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    intval() would work in this specific case because the number is the first thing in your string.

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wouldn't this be enough?
    PHP Code:
    echo substr("123 anywhere street"03); 

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it would unless its
    1234 anywhere street


    other (possibly more robust) solutions could be a regular expression, or sscanf()

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    it would unless its
    1234 anywhere street
    Somehow, i am not getting what u are saying. would you mind explaining?

  6. #6
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Aidin
    I would like to extract "123" from the variable "123 anywhere street"

    Any ideas?
    This code should do the job:
    PHP Code:
    <?php

    $text 
    '123 anywhere street';
    list(
    $var) = explode(' '$text2);
    print 
    $var;

    ?>
    Quote Originally Posted by Kailash Badu
    Quote Originally Posted by clamcrusher
    it would unless its
    1234 anywhere street
    Somehow, i am not getting what u are saying.
    I think, he meant that your code will work only if the desired part of string is 3 characters long.

  7. #7
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you can define all the types of address someone might be able to help you.

    Whatabout:

    1234a somewhere st
    The Gables, somewhere st

    I mean, where is the information coming from?
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  8. #8
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by earl-grey
    I think, he meant that your code will work only if the desired part of string is 3 characters long
    Well in that case, this will work:
    PHP Code:
    $text "12341245 anywhere street";
    echo 
    substr($text0strpos($text," ")); 


Bookmarks

Posting Permissions

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