SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Apr 10, 2007, 06:29 #1
- 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!
-
Apr 10, 2007, 06:49 #2
- 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.
-
Apr 10, 2007, 07:04 #3
- Join Date
- Feb 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Apr 10, 2007, 08:32 #4
- 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
-
Apr 10, 2007, 08:42 #5
- Join Date
- Feb 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Apr 10, 2007, 08:44 #6
- 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);
-
Apr 10, 2007, 10:53 #7PHP Code:
$url = 'http://www.website.com/car02';
preg_match('#/([a-z]+)(\d+)$#i', $url, $matches);
$name = $matches[1];
$id = $matches[2];
-
Apr 10, 2007, 11:05 #8
- Join Date
- Feb 2007
- Posts
- 54
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
WOW! Thanks Kigoobe & aamonkey!
Bookmarks