【问题标题】:PHP String: remove "\newline" after "\end{Figure}"PHP 字符串:在“\end{Figure}”之后删除“\newline”
【发布时间】:2013-08-24 04:11:48
【问题描述】:

我有大约 4000 个字符的 PHP 字符串;如果在这个 PHP 字符串 $input1、$input2 或 $input3 中的任何位置被识别,我想用 $output 字符串替换它。总之,我想在“\end{Figure}”之后删除“\newline”

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

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

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

【问题讨论】:

    标签: php string replace preg-replace


    【解决方案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 
    ~
    

    【讨论】:

    • 完美......你是天才......这正是我正在寻找的。谢谢
    • 感谢您提供模式分解;这非常有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多