2015-11-07 65 views
0

我正在尝试使用preg_match_all来匹配......之间的任何内容,并且该行进行自动换行。我已经完成了谷歌搜索数量,并尝试不同的组合,没有任何工作。我已经试过这php preg_match_all between ...和

preg_match_all('/...(.*).../m/', $rawdata, $m); 

下面是一个什么样的格式看起来像一个例子:

...this is a test... 

...this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test... 
+0

如有任何疑问或问题与答案?如果不是,而且一个适合你,请务必接受它。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – chris85

回答

0

请逃避字面点,因为性格也是一个正则表达式reservered标志,当您使用它的代码里面自己:

preg_match_all('/\.\.\.(.*)\.\.\./m/', $rawdata, $m) 

如果你想什么状态是,有内容中换行符匹配你必须明确地添加到您的代码:

preg_match_all('/\.\.\.([.\n\r]*)\.\.\./m/', $rawdata, $m) 

检查这里以供参考哪些字符点包括: http://www.regular-expressions.info/dot.html

0

你几乎接近得到它,

,所以你需要更新你的RE

/\.{3}(.*)\.{3}/m 

RE分解

/:开始串

\. /结束:比赛.

{3}:严丝合缝3(在这种情况下严丝合缝3点)

(.*)匹配任何条件的第一场比赛后到来(...

m:匹配超过多行的字符串。

,当你把所有的东西放在一起,你就会有这种

$str = "...this is a test..."; 
preg_match_all('/\.{3}(.*)\.{3}/m', $str, $m); 
print_r($m); 

输出

Array 
(
    [0] => Array 
     (
      [0] => ...this is a test... 
     ) 

    [1] => Array 
     (
      [0] => this is a test 
     ) 

) 

DEMO

1

s修改允许.,包括新所以请尝试以下行字符:

preg_match_all('/\.{3}(.*?)\.{3}/s', $rawdata, $m); 

你正在使用的m修改是为了让^$作用于每行​​的基础上,而不是每串(因为你没有^$没有意义)。

你可以阅读更多关于修饰符here

注意.也需要逃脱,因为它是一个特殊字符,意思是any character.*之后的?使其非贪心,因此它将匹配找到的第一个...{3}说三个前面的字符。

Regex101 demo:https://regex101.com/r/eO6iD1/1