2016-11-17 1 views
1

Ich habe ein paar mögliche doppelte Fragen gelesen, aber ich denke nicht, dass sie dieses Problem angehen.preg_match funktioniert nicht mit Variable von strip_tags

Im folgenden Testcode funktioniert das 4. preg_match nicht. Es funktioniert jedoch, wenn Sie die dritte Zeile unkommentieren und den Text verwenden, den ich aus dem Echo kopiert und eingefügt habe.

<?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: [email protected] 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/>'; 


?> 

Der Inhalt von testhtml.txt ist unten. Ich frage mich, ob die Zeile bricht nach "Ich möchte:" verursachen Probleme, aber ich habe auch versucht preg_replace("/\r|\n/", "", $text);, um es zuerst ohne Glück zu löschen.

Meine Frage ist: Warum funktioniert das nicht und/oder was ist der Unterschied zwischen den beiden Versionen von $ 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: [email protected]</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

Können Sie eine MVCE des Problems veröffentlichen? Zu testende Zeichenfolge und die erforderlichen Zeilen, um das Problem zu reproduzieren. Das '.' passt nicht zu neuen Zeilen, es sei denn, der' s' Modifikator wird verwendet. '$ text' ist ein One Liner, also denke ich, dass könnte das Problem sein, unsicher, aber ohne ein klareres Beispiel .. – chris85

Antwort

0

Das Problem war, dass strip_tags mich immer noch mit vieler Linie links bricht, obwohl diese nicht gezeigt wurden, als Echo. Ich habe dies gefunden echo "<pre>" . $text . '</pre>';

So meine Lösung, um die angegebenen Ausdrücke zu arbeiten war einfach $text = preg_replace("/\r|\n/", "", $text); hinzufügen, um alle Zeilenumbrüche zu entfernen.

+0

So ignoriert Kommentar? – chris85

+0

Offensichtlich, ja. Entschuldigung, ich habe es nicht gesehen. Was ist ein MVCE? – Warren

+0

Es ist die einfachste Version Ihres Codes, um das Problem http://stackoverflow.com/help/mcve zu reproduzieren. Probieren Sie den 's' Modifier und Sie sollten nicht die' preg_replace' benötigen. – chris85