2
A
回答
5
你想在键或键和值上做这个或只是保留键和处理值?无论哪种情况,array_combine()
,array_keys()
和array_values()
都可以结合使用。
上的按键:
$keys = array_keys($input);
$values = array_values($input);
$result = preg_replace($pattern, $replacement, $keys);
$output = array_combine($result, $values);
上的按键和值:
$keys = array_keys($input);
$values = array_values($input);
$newKeys = preg_replace($pattern, $replacement, $keys);
$newValues = preg_replace($pattern, $replacement, $values);
$output = array_combine($newKeys, $newValues);
上的值保持键:
$keys = array_keys($input);
$values = array_values($input);
$result = preg_replace($pattern, $replacement, $values);
$output = array_combine($keys, $result);
所有这些假设的功能是这样的:
function regex_replace(array $input, $pattern, $replacement) {
...
return $output;
}
1
如果我理解这个正确的,你想要的是:
$patterns = array_keys($input);
$replacements = array_values($input);
$output = preg_replace($patterns,$replacements,$string);
0
花的时间相当位在这等会增加我的回答
$str = "CT is right next to NY";
$list = array('CT'=>'connenticut', 'NY'=>'New York', 'ME' => 'Maine');
$list = get_json();
$pattern = array_keys($list);
$replacement = array_values($list);
foreach ($pattern as $key => $value) {
$pattern[$key] = '/\b'.$value.'\b/i';
}
$new_str = preg_replace($pattern,$replacement, $str);
请确保您使用/ delimiters/for正则表达式模式匹配。 \ b是用于文字边界。
相关问题
- 1. 的preg_replace与关联数组
- 2. 仅使用preg_replace()或组合preg_match()和preg_replace()?
- 3. 使用关联数组
- 4. preg_replace/e和数组
- 5. ColdFusion和关联数组
- 6. SimpleXML,关联数组和XPath
- 7. 关联数组和循环
- 8. 的Prototype.js和关联数组
- 9. javascript多维数组和关联数组
- 10. 使用关联和外键组合findAll
- 11. 关联数组
- 12. 关联数组
- 13. preg_replace和数组不工作
- 14. php关联数组和开关
- 15. 使用关联数组键和数值数组值创建一个数组PHP
- 16. Dlang数组关联数组
- 17. 使用NSKeyedArchiver保存关联数组
- 18. 使用PL/SQL关联数组
- 19. 在关联数组中使用变量
- 20. 使用Javascript - 多维关联数组
- 21. PHP关联数组引用
- 22. 左联合关联数组
- 23. JavaScript关联数组
- 24. TypeScript关联数组
- 25. 关联数组C
- 26. Ajax关联数组
- 27. Flex关联数组
- 28. 关联数组 “.keys()”
- 29. PHP关联数组
- 30. “Array_combine”关联数组
请参阅此链接[Pre_REPLACE与Asscoiate阵列] [1] [1]:http://stackoverflow.com/questions/17979011/use-preg-replace-to-replace-whole-words - 使用相联阵列 – 2015-03-09 02:42:37