【发布时间】:2017-01-23 05:29:13
【问题描述】:
是否可以使用PHP preg_replace 来获取每一行的值并用下一行的值替换它?例如:
id "text 1"
str ""
id "text 2"
str ""
id "text 6"
id_p "text 6-2"
str[0] ""
str[1] ""
结果
id "text 1"
str "text 1"
id "text 2"
str "text 2"
id "text 6"
id_p "text 6-2"
str[0] "text 6"
str[1] "text 6-2"
我使用正则表达式,但我无法做到这一点,我不确定它是否可能仅使用正则表达式。
感谢任何帮助或指导。
【问题讨论】:
-
你说你正在使用正则表达式,你能分享一下吗?另外,如果
str值为空,您不只是尝试用前面的id值填充str值吗? -
是的,没错
-
为什么要使用
preg_replace? -
你的格式是这样的吗?我的意思是 2 行,然后总是空的?
-
@PatrickMlr 我还能用什么,因为它是关于替换,我认为它是最好的。
标签: php regex preg-replace preg-replace-callback