简体   繁体   English

preg_match不使用strip_tags生成的变量

[英]preg_match not working with variable made by strip_tags

I've read a few possible duplicate questions, but I don't think they address this problem. 我已经阅读了一些可能的重复问题,但我不认为它们可以解决这个问题。

In the below test code, the 4th preg_match does not work. 在下面的测试代码中,第4个preg_match不起作用。 It does, however, work if you un-comment the third line and use the text which what I copied and pasted from the echo. 但是,如果您取消注释第三行并使用我从回声中复制和粘贴的文本,它确实有效。

<?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/>';


?>

The contents of testhtml.txt is below. testhtml.txt的内容如下。 I'm wondering if the line breaks after "I would like to:" are causing problems, but I've also tried preg_replace( "/\\r|\\n/", "", $text ); 我想知道在“我想:”之后线路是否会导致问题,但我也尝试了preg_replace( "/\\r|\\n/", "", $text ); to clear it up first with no luck. 首先清除它,没有运气。

My question is: why is this not working and/or what is the difference between the two versions of $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>

The issue was that strip_tags still left me with a lot of line breaks even though these weren't shown when echoing. 问题是strip_tags仍然给我留下了很多换行符,即使strip_tags时没有显示这些换行符。 I found this by doing echo "<pre>" . $text . '</pre>'; 我通过echo "<pre>" . $text . '</pre>';找到了这个echo "<pre>" . $text . '</pre>'; echo "<pre>" . $text . '</pre>';

Thus my solution to work the expressions given was to simply add $text = preg_replace( "/\\r|\\n/", "", $text ); 因此,我使用给出的表达式的解决方案是简单地添加$text = preg_replace( "/\\r|\\n/", "", $text ); to remove all the line breaks. 删除所有换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM