Reputation: 599
I have a string which contains one or more integer numbers separated by a space character, for example:
$string = '2 7 6 9 11';
I want to replace each number with the corresponding word which is stored in an array, for example:
static $companyTypes = array('word1', 'word2', 'word3', 'word4', 'word5', 'word6', 'word7', 'word8', 'word9', 'word10', 'word11', 'word12');
So I used example that I found in this page: http://php.net/manual/en/function.preg-replace.php
and I defined a pattern array like this:
$pattern = array('/1/','/2/','/3/','/4/','/5/','/6/','/7/','/8/','/9/','/10/','/11/','/12/');
and finally used preg_replace function like this:
$order->company_type= preg_replace($pattern, $companyTypes, $order->company_type);
but unfortunately this solution will not differentiate between numbers with one digit and numbers with two digits so if the input string is '1 11' the output will be 'word1 word1word1' and not 'word1 word11'.
Any help will be appreciated.
Upvotes: 2
Views: 456
Reputation: 4760
A solution that is entirely regular expression:
$pattern = array('/(^1 | 1 | 1$)/', '/(^2 | 2 | 2$)/', '/(^3 | 3 | 3$)/', '/(^4 | 4 | 4$)/', '/(^5 | 5 | 5$)/' , '/(^6 | 6 | 6$)/', '/(^7 | 7 | 7$)/', '/(^8 | 8 | 8$)/', '/(^9 | 9 | 9$)/', '/(^10 | 10 | 10$)/', '/(^11 | 11 | 11$)/', '/(^12 | 12 | 12$)/', '/(^13 | 13 | 13$)/', '/(^14 | 14 | 14$)/');
echo preg_replace($pattern, $companyTypes, $string);
What /(^5 | 5 | 5$)/
means that if a string beings with a 5 followed by a space, or if we match a string that has a ' 5 ', or if we match a string that is at the end of the string and it is preceded by a space then it will match. It will match '5 '
(at the beginning of the string), ' 5 '
(any where in the middle), or ' 5'
(at the end of the string).
If one of the company types matches something in the regular expression you might have the problems you originally described. So I have provided another solution if you really need it.
Another solution that splits the string apart:
The regular expression can be updated to only replace on an exact match.
$pattern = array('/^1$/','/^2$/','/^3$/','/^4$/','/^5$/','/^6$/','/^7$/','/^8$/','/^9$/','/^10$/','/^11$/','/^12$/','/^13$/','/^14$/');
So, in the example of /^10$/
, the ^
indicates the start of the string and the $
indicates the end of the string. Altogether it means it means if there is an exact match of 10.
And, you should really split up your starting to prevent any undesired string changes. So, use explode
to split your string up then iterate over each string part and replace the desired parts then bring the string back together with implode
.
$string = '2 7 6 9 11';
$string_parts = explode(' ', $string);
$pattern = array('/^1$/','/^2$/','/^3$/','/^4$/','/^5$/','/^6$/','/^7$/','/^8$/','/^9$/','/^10$/','/^11$/','/^12$/','/^13$/','/^14$/');
$result = [];
foreach ($string_parts as $string_part) {
$result[] = preg_replace($pattern, $companyTypes, $string_part);
}
$order->company_type = implode(' ', $result);
Upvotes: 1