Mcod Posted September 11, 2012 Share Posted September 11, 2012 I am having issues with replacing strings - see example below: function getname($string) { $search = array('0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22'); $replace = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v"); return str_replace($search, $replace, $string); } If I run getname(22); then it returns cc instead of v, meaning it replaces the 22 with two times the 2 Your help would be appreciated Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 You would have to run it backwards, but then what happens when you WANT cc instead of v? Or want cb and that's 21, and you replace it with u? You need to zerofill your numbers, or use a deliminator. Why are you doing this? Quote Link to comment Share on other sites More sharing options...
Mcod Posted September 11, 2012 Author Share Posted September 11, 2012 Thank you for your reply. The reason why I need to do this is because I had a script coded and there are category numbers which I want to replace with words. There are 40 categories, so I looked up some ways to solve this, but as you can see, it causes more pain than it does good Any recommendation how to solve this with a similar function that actually works? Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 What do you mean you want to replace with words? How do YOU as a human know what letter(s) to replace 22 with? How do you know it's supposed to be v and not cc? Quote Link to comment Share on other sites More sharing options...
Mcod Posted September 11, 2012 Author Share Posted September 11, 2012 For me it's simple, I have a value like 22 and to me it's clear that "22" means "Cars" and "2" means "Business". So what I get from my mysql database is the value 22 which means the listing is for the category "Cars" and a result with 2 would be the category "Business". Since I have to print out the category in human-readable form instead of showing "Category: 22" I am looking for a function to convert the number 22 into the word "Cars" - right now (as you know) it would echo BusinessBusiness which is not what I want Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 11, 2012 Share Posted September 11, 2012 ... How on earth does 22 == cars and 2 = business? you said 22 = v How many categories are there? You should have a table which has each category and it has an ID, which it SOUNDS like you sort of already have - and then store the category id with each product, and join the tables. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.