Preg_match对strip_tags生成的变量不起作用


preg_match not working with variable made by strip_tags

我读了一些可能重复的问题,但我不认为它们解决了这个问题。

在下面的测试代码中,第4个preg_match不起作用。但是,如果您取消注释第三行并使用我从回显中复制并粘贴的文本,则可以正常工作。

<?php
$html = file_get_contents('testhtml.txt');
$text = strip_tags($html);
//$text = 'body{font-family: arial,helvetica,sans-serif;} a{color: #06c;} p{margin:0;} #message{width:600px;margin:0 auto;} .legal{margin-top:2em;} .footer{margin-top:1em;padding:5px;background:#999999;color:#fff;} .footer a{color:#fff;} .senderName,.label{font-weight:bold;} .link,.label,.hint{margin-top: 20px;} .header-separator{height:4px;background-color:#e4002b;width:100%;margin-top:17px;} tr,td{vertical-align:top;text-align:left;} img{border:0;} Property id: 416848282 Property address: 12/41 Fake Road, Town, Vic 3000 Property URL: http://myurl.comu User Details: Name: Warren Warren Email: warren@warren.com.au Phone: (03) 1234 7777 I would like to: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce aliquet purus ac ullamcorper condimentum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Comments: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce aliquet purus ac ullamcorper condimentum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla nec orci magna. Quisque facilisis aliquet massa eu feugiat. Mauris eleifend elit aliquet mi egestas, eu gravida augue tempus. Sed libero nunc, euismod non nisl nec, vehicula laoreet dolor. Suspendisse sed convallis diam, non porta arcu. Remember, you can only use the personal information contained in this email enquiry for the purposes of contacting the person about their property enquiry Contact Number: 9999999999 (8.00am - 7.00pm ESDST) Message sent from http://myurl.comu ';

echo $text . '<br /><br />';
$re = '/(?<=Name: )(.*?)(?= Email:)/s';
preg_match($re, $text, $matches);
print_r($matches);
echo '<br/>';
$re = '/(?<=Email: )(.*?)(?= Phone:)/s';
preg_match($re, $text, $matches);
print_r($matches);
echo '<br/>';
$re = '/(?<=Phone: )(.*?)(?= I would like to:)/s';
preg_match($re, $text, $matches);
print_r($matches);
echo '<br/>';
$re = '/(?<=I would like to: )(.*?)(?=Comments:)/s';
preg_match($re, $text, $matches);
print_r($matches);
echo '<br/>';
$re = '/(?<=Comments: )(.*?)(?= Remember, you can)/s';
preg_match($re, $text, $matches);
print_r($matches);
echo '<br/>';

?>

testtml .txt的内容如下。我想知道"我想:"之后的断行是否会引起问题,但我也尝试过preg_replace( "/'r|'n/", "", $text );首先清除它,但没有运气。

我的问题是:为什么这不起作用和/或$text的两个版本之间的区别是什么?

<html>
<head>
<style type='text/css'>
body{font-family: arial,helvetica,sans-serif;}
a{color: #06c;}
p{margin:0;}
#message{width:600px;margin:0 auto;}
.legal{margin-top:2em;}
.footer{margin-top:1em;padding:5px;background:#999999;color:#fff;}
.footer a{color:#fff;}
.senderName,.label{font-weight:bold;}
.link,.label,.hint{margin-top: 20px;}
.header-separator{height:4px;background-color:#e4002b;width:100%;margin-top:17px;}
tr,td{vertical-align:top;text-align:left;}
img{border:0;}
</style>
</head>
<body>
<div id='message'>



    <BR/>
    <P>Property id: 416848282</P>
    <P>Property address: 12/41 Fake Road, Town, Vic 3000</P>
    <P>Property URL: <a href="http://myurl.com">http://myurl.comu</a></P>
    <BR/>
    <P>User Details:<P>
    <P>Name: Warren Warren</P>
    <P>Email: warren@warren.com.au</P>
    <P>Phone: (03) 1234 7777</P>
    <P>I would like to:
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce aliquet purus ac ullamcorper condimentum. Interdum et malesuada fames ac ante ipsum primis in faucibus.
    </P>
    <P>Comments: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce aliquet purus ac ullamcorper condimentum. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nulla nec orci magna. Quisque facilisis aliquet massa eu feugiat. Mauris eleifend elit aliquet mi egestas, eu gravida augue tempus. Sed libero nunc, euismod non nisl nec, vehicula laoreet dolor. Suspendisse sed convallis diam, non porta arcu. </P>
<div class="legal" style="color:#888;margin-top:140px;font-size:12px">
    <p>Remember, you can only use the personal information contained in this email enquiry for the purposes of contacting the person about their property enquiry</p>
    <br>
    <p>Contact Number: 9999999999 (8.00am - 7.00pm ESDST)</p>
</div>
    <div class="footer">
Message sent from <a href="http://myurl.com">http://myurl.comu</a>    </div>
</div>
</body>
</html>

问题是strip_tags仍然给我留下了很多换行符,即使这些在回显时没有显示。我发现这是通过做echo "<pre>" . $text . '</pre>';

因此,我的解决方案是简单地添加$text = preg_replace( "/'r|'n/", "", $text );来删除所有的换行符。