如何使用preg_replace将子字符串替换为数组中的字符串?(PHP)

约翰·波

我有一个包含这些值的数据数组:

Array
(
    [id] => 1
    [myid] => 9
    [date] => 2014-01-30
    [user] => 17
    [reason] => some text here...
)

并且此字符串包含引用数据数组索引的数字:

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

是否可以将括号中的数字(包括括号)更改为数组中的适当值?

我知道如何使用,(string) preg_replace( (array) $patterns, (array) $replacements, (string) $subject)但完全不知道如何解决此问题。

理想情况下,结果字符串应如下所示:

'1' as "id",'9'  as "myid",'2014-01-30'  as "date",'17'  as "user",'some text here...'  as "reason"
罗曼·佩列赫雷斯特(RomanPerekhrest)

该解决方案的使用preg_replace_callbackstr_replace功能:

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

// $arr is your initial array
$result = preg_replace_callback(
                "/(\(\d\)) as \"(\w+?)\",?/i",
                function ($maches) use($arr){
                     return str_replace([$maches[1]], "'". $arr[$maches[2]]. "'", $maches[0]);
                },
                $columns);

print_r($result);

输出:

'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用preg_replace()和regex将大写字符串替换为小写

来自分类Dev

preg_replace替换子字符串php中的数字

来自分类Dev

使用preg_replace()将URL转换为字符串中的链接

来自分类Dev

在 preg_replace 中使用子字符串

来自分类Dev

(PHP)如何使用值数组替换字符串中的不同子字符串?

来自分类Dev

如何使用gulp replace将字符串替换为数组中的值

来自分类Dev

如何用str_replace或preg_replace的php字符串替换单词

来自分类Dev

在字符串中包含php preg_replace

来自分类Dev

字符串 PHP 中的 preg_replace 常量

来自分类Dev

用preg_replace替换字符串中的数组变量

来自分类Dev

如何用preg_replace在字符串中给空格?

来自分类Dev

使用preg_replace从字符串中删除Â

来自分类Dev

使用 preg_replace 转换字符串中的 URL

来自分类Dev

Laravel:preg_replace():参数不匹配,模式是字符串,而替换是数组

来自分类Dev

laravel 5.2:ErrorException:preg_replace():参数不匹配,模式是字符串,而替换是数组

来自分类Dev

给定PHP preg_replace字符串路径

来自分类Dev

PHP preg_replace字符串到图像

来自分类Dev

PHP preg_replace仅在特定子字符串之后才替换更多空格

来自分类Dev

PHP:preg_replace函数,用于替换字符串范围内的unicode chracters

来自分类Dev

preg_replace删除特殊字符串中的内容

来自分类Dev

Preg_replace字符串中的所有模式

来自分类Dev

preg_replace函数从字符串中删除空格

来自分类Dev

Preg_replace 在字符串中查找数字

来自分类Dev

使用preg_replace转换字符串

来自分类Dev

使用preg_replace“更新”搜索到的字符串

来自分类Dev

使用preg_replace模式清理字符串

来自分类Dev

如何使用位置将 Swift 子字符串替换为 Un-Equal 子字符串?

来自分类Dev

preg_replace只替换字符串中的第一个数字

来自分类Dev

将数组中的空字符串替换为0

Related 相关文章

  1. 1

    使用preg_replace()和regex将大写字符串替换为小写

  2. 2

    preg_replace替换子字符串php中的数字

  3. 3

    使用preg_replace()将URL转换为字符串中的链接

  4. 4

    在 preg_replace 中使用子字符串

  5. 5

    (PHP)如何使用值数组替换字符串中的不同子字符串?

  6. 6

    如何使用gulp replace将字符串替换为数组中的值

  7. 7

    如何用str_replace或preg_replace的php字符串替换单词

  8. 8

    在字符串中包含php preg_replace

  9. 9

    字符串 PHP 中的 preg_replace 常量

  10. 10

    用preg_replace替换字符串中的数组变量

  11. 11

    如何用preg_replace在字符串中给空格?

  12. 12

    使用preg_replace从字符串中删除Â

  13. 13

    使用 preg_replace 转换字符串中的 URL

  14. 14

    Laravel:preg_replace():参数不匹配,模式是字符串,而替换是数组

  15. 15

    laravel 5.2:ErrorException:preg_replace():参数不匹配,模式是字符串,而替换是数组

  16. 16

    给定PHP preg_replace字符串路径

  17. 17

    PHP preg_replace字符串到图像

  18. 18

    PHP preg_replace仅在特定子字符串之后才替换更多空格

  19. 19

    PHP:preg_replace函数,用于替换字符串范围内的unicode chracters

  20. 20

    preg_replace删除特殊字符串中的内容

  21. 21

    Preg_replace字符串中的所有模式

  22. 22

    preg_replace函数从字符串中删除空格

  23. 23

    Preg_replace 在字符串中查找数字

  24. 24

    使用preg_replace转换字符串

  25. 25

    使用preg_replace“更新”搜索到的字符串

  26. 26

    使用preg_replace模式清理字符串

  27. 27

    如何使用位置将 Swift 子字符串替换为 Un-Equal 子字符串?

  28. 28

    preg_replace只替换字符串中的第一个数字

  29. 29

    将数组中的空字符串替换为0

热门标签

归档