English 中文(简体)
需要使用PHP正则表达式来替换换行符,但只能替换引号之间的换行符。
原标题:PHP regular expression needed to replace line breaks, but only those between quotes

我们的客户向我们提供了需要使用PHP处理的XML数据。他们选择滥用属性,将其用于大块文本(包含换行符)。XML解析器将换行符替换为空格,以使XML符合W3标准。

为了确保我们不会失去我们的分界线,我想在档案中读作一个插图,然后翻译与<代码>和amp;第13号;之间的所有分界线。 我认为,我需要定期表达这一点,但我正与大家一起努力。

这是我迄今为止的测试代码(PHP 5),使用向前和向后查找,但不起作用:

$xml =  <tag attribute="Header

First paragraph.">
</tag> ;
$pattern =  /(?<=")([^"]+?)
([^"]+?)(?=")/ ;

print_r( preg_replace($pattern, "$1&#13;$2", $xml) );

有人能帮我弄对吗?对于一个有经验的正则表达式大师来说应该很容易:)

问题回答

最好的方法是逐个寻找特性。 如果你碰到一个引号,那么,当你发现相片时,就会弄错。

如果您在引号内(即您的变量为 true)找到换行符,则“用 &#13; 进行翻译”,不然就保持不变。

确切地说,这就是我最终得到的结果。为了将来参考,我将在这里发布可用的代码:

<?php
    header("Content-Type: text/plain");

    $xml =  <tag attribute="Header

First paragraph.">
</tag> ;

    // split the contents at the quotes
    $array = preg_split( /["]+/ , $xml);

    // replace new lines in each of the odd strings parts
    for($i=1;$i<count($array);$i+=2){
        $array[$i] = str_replace( 

 , &#13; ,$array[$i]);
        $array[$i] = str_replace( 
 , &#13; ,$array[$i]);
        $array[$i] = str_replace( 
 , &#13; ,$array[$i]);
        $array[$i] = str_replace( 
 , &#13; ,$array[$i]);
    }

    // reconstruct the original string
    $xml = implode( " , $array);

    print_r( $xml );
?>

感谢回复并支持此解决方案 :)





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签