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!
| SitePoint Sponsor |
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!





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![]()





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![]()





If you want your visitor to type http://www.website.com/Fred6598, then you can use something like
You will get http://www.website.com/Fred#6598 then you can use an explode to seperate themPHP Code:$url = "http://www.website.com/Fred6598";
$url = preg_replace("/([a-zA-Z])([0-9])/","$1#$2",$url);
PHP Code:$url = 'http://www.website.com/car02';
preg_match('#/([a-z]+)(\d+)$#i', $url, $matches);
$name = $matches[1];
$id = $matches[2];
WOW! Thanks Kigoobe & aamonkey!
![]()
Bookmarks