Ich habe eine TXT-Datei, die mit HTML-Code darin gefüllt ist. Ich versuche, eine PHP-Seite zu erstellen, die den Code sucht und erhält den „username“ für mich:Benötigen Sie Hilfe, Zeichenfolge aus einer Datei in PHP
Hier ist eine kleine Auswahl der Seite:
<div class="search-result-details">
<div class="employee-name">This is my name!</div>
<ul class="employee-details">
<li><span class="label">Login</span>username</li>
<li><span class="label">Employee ID</span>####</li>
<li><span class="label">Barcode ID</span>###</li>
<li><span class="label">Status</span>Active</li>
</ul>
<ul class="org-details">
<li><span class="label">Location</span>SAT1 (755)</li>
<li><span class="label">Shift</span>AAAA</li>
<li><span class="label">Department</span>1231</li>
<li><span class="label">Area</span>26</li>
<li><span class="label">Crew</span>0</li>
<li><span class="label">Supervisor</span>manager name</li>
</ul>
</div>
</a></li>
</ol>
</div>
und ich brauche den Benutzernamen greifen die folgende Zeile:
<li><span class="label">Login</span>username</li>
ich dies bereits, dass zumindest packt die Linie, die ich brauche:
<?php
$file = 'log.txt';
$searchfor = '<ul class="employee-details">
<li><span class="label">Login</span>';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
?>
Stromausgang:
<ul class="employee-details">
<li><span class="label">Login</span>username</li>
Jede Hilfe wird sehr geschätzt. Vielen Dank.
Ich bevorzuge es, HTML-Dateien mit * DOMDocument * und * DOMXpath * zu analysieren. Vielleicht wird es auch dir gehören. – donald123