SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Seperating characters from numbers in a single string...

    Hi,

    I have a string, for example: "Cars02"

    What I want to do, is separate "Cars" from "02" and make them each a variable so I can use them later on in my code.

    How can I do this?

    Thanks!

  2. #2
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A better idea is to store them, whem you store, with something in between, like car#02 in the database, in that case, you can easily seperate or unite them as per your need. Else, regexp is the way.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe View Post
    A better idea is to store them, whem you store, with something in between, like car#02 in the database, in that case, you can easily seperate or unite them as per your need. Else, regexp is the way.
    Well in the database I have the name, and ID number. They are separate fields. I have people calling that row by entering the name + id, so when I query the database, I need to split the two and match them with their row.

    So the user might enter: http://www.website.com/car02

    I need to split it and then use WHERE id='02' and name='car'

    Any idea how I can do this with regexp? I am not very familiar with regular expressions at all

  4. #4
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If that's the case, and you are using php, why don't simply use a url like http://www.website.com/page.php?vehicle=car&id=2 It won't be tough for having them seperated instead of using car02 when people calls that row. If you give that part of your code, I'm sure someone here can give u a nice idea about how to deal with that, without even needing regexp

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe View Post
    If that's the case, and you are using php, why don't simply use a url like http://www.website.com/page.php?vehicle=car&id=2 It won't be tough for having them seperated instead of using car02 when people calls that row. If you give that part of your code, I'm sure someone here can give u a nice idea about how to deal with that, without even needing regexp
    Well it's going to be using peoples names, and their ID in the database. They're going to be handed out, so it needs to be a simple URL they can type into their browser. http://www.website.com/Fred01 would be easier for someone to type in than http://www.website.com/user.php?name=fred&id=1

    I'm going to keep searching. Thanks for the help so far

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want your visitor to type http://www.website.com/Fred6598, then you can use something like
    PHP Code:
    $url "http://www.website.com/Fred6598"
    $url preg_replace("/([a-zA-Z])([0-9])/","$1#$2",$url); 
    You will get http://www.website.com/Fred#6598 then you can use an explode to seperate them

  7. #7
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $url 'http://www.website.com/car02';

    preg_match('#/([a-z]+)(\d+)$#i'$url$matches);

    $name $matches[1];
    $id $matches[2]; 

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! Thanks Kigoobe & aamonkey!



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
  •