Basically from a database I am getting data that is formatted like this nameofproject101 Now this could continue to increase so eventually it could be nameofproject1001 my question is how can I trim off the number and just get the name of the project. I thought about using substr but since I dont know the length always I cant really do that. Since the numbers differ I dont think I can use str_replace is there any way to accomplish this?
-
Use regular expression to strip out trailing numbers. stackoverflow.com/questions/2123334/…thenetimp– thenetimp2012-02-05 18:02:18 +00:00Commented Feb 5, 2012 at 18:02
-
Will the name of the project itself contain numbers?erisco– erisco2012-02-05 18:03:03 +00:00Commented Feb 5, 2012 at 18:03
-
@erisco From the database yes it is stored like 'nameofproject101' I need it to be 'nameofproject', but what I need to do with the data, I dont need the numbers so I need them stripped off.atrljoe– atrljoe2012-02-05 18:04:29 +00:00Commented Feb 5, 2012 at 18:04
-
@atrljoe I believe erisco was asking whether the name (without the trailing numbers) could contain numbers. E.g. r2d2translator1001.kba– kba2012-02-05 18:11:52 +00:00Commented Feb 5, 2012 at 18:11
Add a comment
|
4 Answers
It sounds like something is way off about your database scheme. You should probably try to do refactor/normalize your scheme.
But in the meantime, you can use rtrim() to trim all numbers off of the right side.
$val = rtrim($val, '0123456789');
Examples
Input Output
nameofproject1001 nameofproject
nameofproject nameofproject
n4me0fproj3ct1001 n4me0fproj3ct
Comments
You should definitely use regular expressions:
$fullname = "nameofproject101";
preg_match("/([a-z]+)([0-9]+)/i", $fullname, $matches);
$name = $matches[1];
$number = $matches[2];
echo "'$fullname' is '$name' followed by '$number'";
1 Comment
kba
This will turn
version2project101 into version. OP probably wants version2project.