使用preg_replace将整数映射到字符串

时间:2023-02-12 08:45:39

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

所以我使用了我在此页面中找到的示例: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:

最后使用preg_replace函数,如下所示:

$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'.

但不幸的是,这个解决方案不会区分一位数字和两位数字,所以如果输入字符串是'111',输出将是'word1 word1word1'而不是'word1 word11'。

Any help will be appreciated.

任何帮助将不胜感激。

1 个解决方案

#1


1  

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).

什么/(^ 5 | 5 | 5 $)/意味着如果一个字符串有一个5后跟一个空格,或者我们匹配一个带有'5'的字符串,或者我们匹配一个结尾的字符串字符串的前面是一个空格然后匹配。它将匹配'5'(在字符串的开头),'5'(在中间的任何位置)或'5'(在字符串的末尾)。

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.

因此,在/ ^ 10 $ /的示例中,^表示字符串的开头,$表示字符串的结尾。总而言之,这意味着它意味着是否存在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.

而且,你应该真正分开你的开始,以防止任何不希望的字符串更改。因此,使用explode将你的字符串分开然后遍历每个字符串部分并替换所需的部分然后将字符串与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);

#1


1  

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).

什么/(^ 5 | 5 | 5 $)/意味着如果一个字符串有一个5后跟一个空格,或者我们匹配一个带有'5'的字符串,或者我们匹配一个结尾的字符串字符串的前面是一个空格然后匹配。它将匹配'5'(在字符串的开头),'5'(在中间的任何位置)或'5'(在字符串的末尾)。

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.

因此,在/ ^ 10 $ /的示例中,^表示字符串的开头,$表示字符串的结尾。总而言之,这意味着它意味着是否存在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.

而且,你应该真正分开你的开始,以防止任何不希望的字符串更改。因此,使用explode将你的字符串分开然后遍历每个字符串部分并替换所需的部分然后将字符串与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);