2016-10-12 5 views
-1

Wie bekomme ich Bildpfad im Textformat mit PHP?Wie bekomme ich Bildpfad im Textformat mit PHP?

Ich habe Textformat wie folgt aus:

<p><span style="\&quot;background-color:" rgb(255,="" 255,="" 0);="" font-weight:="" bold;="" font-style:="" italic;="" text-decoration:="" underline;\"="">TEST</span>&nbsp;<img src="\&quot;https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\&quot;" style="\&quot;width:" 544px;\"="">&nbsp;<span style="\&quot;font-style:" italic;\"="">TEST<img src="\&quot;https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png\&quot;" style="\&quot;width:" 200px;\"=""></span><br></p> 

Mein Code ist:

<?php 
$str='<p><span style="\&quot;background-color:" rgb(255,="" 255,="" 0);="" font-weight:="" bold;="" font-style:="" italic;="" text-decoration:="" underline;\"="">TEST</span>&nbsp;<img src="\&quot;https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\&quot;" style="\&quot;width:" 544px;\"="">&nbsp;<span style="\&quot;font-style:" italic;\"="">TEST<img src="\&quot;https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png\&quot;" style="\&quot;width:" 200px;\"=""></span><br></p>'; 

libxml_use_internal_errors(true); 
$dom=new DOMDocument; 
$dom->validateOnParse=false; 
$dom->standalone=true; 
$dom->strictErrorChecking=false; 
$dom->recover=true; 
$dom->formatOutput=false; 
$dom->loadHTML($str); 
libxml_clear_errors(); 
$xp=new DOMXPath($dom); 
$col=$xp->query('//img'); 
$images=array(); 
foreach($col as $img) $images[]=$img->getAttribute('src'); 
echo $images[0]; 
?> 

Wenn ich meinen Code testen, es zeigt:

\"https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\" 

ich wissen will, wie meinen Code ändern, um zu zeigen:

https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 
+0

Teile dieses Textes wurden durch 'addslashes()' und dann 'htmlentities()' ausgeführt. – AbraCadaver

+0

Mit anderen Worten: Warum ist dieses Stück HTML völlig verstümmelt und irgendwo zwischen Nicht-Was-Sie-Denken-Es-ist und absolut ungültig? – deceze

Antwort

0

echo substr ($ images [0], 2, strlen ($ images [0]) - 5); Wenn ich nicht falsch bin, wenn
$images[0] =\"https://www.google.co.th/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\"

Dann Funktion substr $ Bilder nehmen [0] string in Scheiben geschnitten wird, ist es aus dem Index 2 und erhält strlen ($ Bilder [0]) starten - 5 Stück char die ist strlen ($ images [0]) ist die Größe der Zeichenfolge - 5 dann wird es das gewünschte Ergebnis produzieren.

0

Kann stripslashes verwenden, um das zu entfernen, und wenn Sie HTML-Entitäten in ihren anwendbaren Zeichen möchten, verwenden Sie html_entity_decode.

So sollte Ihr Echo in etwa so aussehen:

echo stripslashes(html_entity_decode($images[0])); 

ODER

echo stripslashes($images[0]); 

je nach, wenn Sie wollen auch HTML-Entities konvertieren.