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与关联数组
- 3. 仅使用preg_replace()或组合preg_match()和preg_replace()?
- 4. 使用关联数组
- 5. 使用关联数组
- 6. preg_replace/e和数组
- 7. JSON.stringify和非关联数组
- 8. 关联数组和循环
- 9. ColdFusion和关联数组
- 10. 关联数组和Java
- 11. 关联数组和XML
- 12. 关联数组和sysdate
- 13. 的Prototype.js和关联数组
- 14. MySQL和PHP关联数组
- 15. SimpleXML,关联数组和XPath
- 16. javascript多维数组和关联数组
- 17. 使用关联和外键组合findAll
- 18. 关联数组
- 19. 关联数组
- 20. preg_replace和数组不工作
- 21. php关联数组和开关
- 22. 使用关联数组键和数值数组值创建一个数组PHP
- 23. 如何使用php和mysql填充和关联数组
- 24. PHP数组关联数组
- 25. Dlang数组关联数组
- 26. 使用PL/SQL关联数组
- 27. 使用jQuery的岗位关联数组
- 28. 使用NSKeyedArchiver保存关联数组
- 29. 如何使用关联数组键
- 30. in_array不能使用关联数组php
请参阅此链接[Pre_REPLACE与Asscoiate阵列] [1] [1]:http://stackoverflow.com/questions/17979011/use-preg-replace-to-replace-whole-words - 使用相联阵列 – 2015-03-09 02:42:37