简体   繁体   中英

Tidy up an image tag

i have this image tag that i'm getting from a weather source that is with errors, the output is not html but wml/wap so it crashes and burns when it shows up. the image tag comes up like this:

<img alt="FACE="Monospace,Courier">LRPatches, Sky clear, Q1020</FONT><Mist, >" src="http://www.vremea.com/images/fogshow.gif"  width="50" height="50"/>

and i would like it to be like this:

<img src="http://www.vremea.com/images/fogshow.gif"  width="50" height="50"/>

i know i have to use preg_replace but I cant seem to make it work, any ideas?

If the HTML always has the exact same syntax problem, this will work to remove anything between <img and src= . This is pretty easy to break if the HTML structure changes, but since it's broken already...

$html = preg_replace('/(?<=<img ).*?(?=src=)/', '', $horribleHorribleHTML);

It's not tested, but this should do it.

<?php
$sStr = '<img ... your image>'; // your string
$iStart = strpos('src="', $sStr); // find the src
$iEnd = strpos('"', $sStr, $iStart); // find the end
$sURL = substr($sStr, $iStart, $iEnd); // get the image
echo $sURL;
?>

You can try to match on the attributes you want to save from your input, you can try to get the parts that look like an <img> tag first, and then cherry-pick the attribute looking parts of them you are interested in:

$input = 'some other content
    <img alt="FACE="Monospace,Courier">LRPatches, Sky clear, Q1020</FONT><Mist, >"
        src="http://www.vremea.com/images/fogshow.gif"  width="50" height="50"/>
        <span class="some"> more other content
    </span>

    <img alt="FACE="Monospace,Courier">LRPatches, Sky clear, Q1020</FONT><Mist, >"
        src="http://www.vremea.com/images/fogshow.gif"
        width="50"
        height="50"/> <span class="some"> more other content
    ';
preg_match_all('/<img.+?\/>/sim', $input, $img_parts);
foreach ($img_parts[0] as $img_part) {
    $attrs = array();
    preg_match_all('/(?<key>src|width|height)\s*=\s*"(?<value>[^"]+)/i', $img_part, $m);
    foreach ($m['key'] as $i => $key) {
        $attrs[] = "{$key}=\"{$m['value'][$i]}\"";
    }
    print "<img ".join(' ', $attrs)." />\n";
}

This :

$imgTag = '<img alt="FACE="Monospace,Courier">LRPatches, Sky clear, Q1020</FONT><Mist, >" src="http://www.vremea.com/images/fogshow.gif"  width="50" height="50"/>';
$returnValue = preg_replace('/(<img)(.*)(src.*)/', '$1 $3',$imgTag);

Will output :

'<img src="http://www.vremea.com/images/fogshow.gif"  width="50" height="50"/>'

Assuming your malformed <img /> tag doesn't change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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