2013-08-24 25 views
0

我PHP字符串大约4000字符;如果在这个PHP字符串$ input1,$ input2或$ input3中的任何地方被识别,我想用$ output字符串替换它。在总结我想删除“\换行”后“\ {结束}图”PHP字符串:删除“结束{图}”后的“新行”

$input1 = "\end{Figure}\newline"; 
$input2 = "\end{Figure} \newline"; 
$input3 = "\end{Figure}\newline " 

Required output: 
$output = "\end{Figure}"; 

能否请你建议如何实现所需的输出?

回答

1

你的问题不是很清楚,但也许这是你在找什么:

$result = preg_replace('~\\\end\{Figure}\K()?\\\newline(?(1)|)~', '', $text); 

图案的详细资料:

~    # pattern delimiter 
\\\    # backslash (must be double escaped inside quotes) 
end\{Figure} 
\K    # reset the begining of the match 
()?    # optional capturing group n°1 
\\\newline 
(?(1)|)   # if capturing group n°1 exists there is nothing else a space 
~ 
+0

完美......你是天才......这正是我在找。谢谢 –

+0

感谢分享模式;这非常有用 –