我已经阅读了一些可能的重复问题,但我认为他们没有解决这个问题。
在下面的测试代码中,第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/>';
?>
testhtml.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>
答案 0 :(得分:0)
问题是strip_tags
仍然留下了很多换行符,即使这些换行时没有显示。我通过echo "<pre>" . $text . '</pre>';
因此,我使用给出的表达式的解决方案是简单地添加$text = preg_replace( "/\r|\n/", "", $text );
以删除所有换行符。