lacas
lacas

Reputation: 14077

Php str_replace with &quote not working

my code is:

        $retval = preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);
        $imgreal    = str_replace("&quot;", "\"", $image['src']);
        if ($retval=="0") $imgthumb = ""; else $imgthumb = "<img align='left' src='".$imgreal."' width=100 />";

I need to extract an img from a string, but this gave me all time:

"http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg&quot; alt=&quot;&quot; width=&quot;490&quot; height=&quot;214

How can I change those &quotes to normal chars "?

I tried htmlspecialchars_decode but that not worked to.

edit: the string coming from az sql table.

original string from mysql:

<p><img style="float: left; margin: 3px;" src="http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214" /></p>
<p>todik p&uacute;ő&uacute;ő&oacute;&uuml;sztotodik p&uacute;ő&uacute;ő&oacute;&uuml;sztotodik 

string created by TinyMCE

coding in the table: latin2_hungarian_ci

Upvotes: 0

Views: 102

Answers (1)

Michel
Michel

Reputation: 4157

Why does everybody always want to do these kind of things with a regex?

Simply:

$data='<p><img style="float: left; margin: 3px;" src="http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg" alt="" width="490" height="214" /></p>
<p>todik p&uacute;ő&uacute;ő&oacute;&uuml;sztotodik p&uacute;ő&uacute;ő&oacute;&uuml;sztotodik';

$a=explode('src="',$data);
if(count($a)<2)
    {
    echo 'no image';
    die;
    }

$p=strpos($a[1],'"',0);
if($p===false){
    echo 'no quote found';
    die;
    }   

$url=substr($a[1],0,$p);

echo $url;

Output:

http://www.racingzone.hu/pictures/news/mid/simroc-3---nyolc-uj-auto_2012-09-21-1348222495.jpg

Upvotes: 1

Related Questions