my code is:
$retval = preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);
$imgreal = str_replace(""", "\"", $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" alt="" width="490" height="214
How can I change those "es 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úőúőóüsztotodik púőúőóüsztotodik
string created by TinyMCE
coding in the table: latin2_hungarian_ci
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úőúőóüsztotodik púőúőóü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
Related
This is main problem of my system I can display images and their names but it cant't be a horizontal.. Need help please. Cause when I run my codes the images will be vertically but I want to be in a horizontal
Here;s the code:
echo'<?php $src=array("candidates/images/".$rows["image"]); for($i=0;$i<2;$i++){ ?>';
echo '<div clas ="image">';
echo '<img src="candidates/images/'.$rows['image'].'" width="10" height="20px" />'.', '.'<br>'.$rows['lastname'].', '.$rows['firstname'].'<br>'.' = '.$rows['votes'];
// echo '<br>';
}
$sdsd=$dsada
?>
<img src="candidates/images/percent.gif"width='<?php echo(100*round($rows['votes']/($sdsd),2)); ?>'height='10'>
<?php
if ($rows['votes']==0){
echo "<br>";}
else {
// echo(100*round($rows['votes']/($sdsd),2)); /
/*?>%<br>*/
/*<?php
}
*/echo '</div>';
}
}
Here's the CSS:
.image {
position: relative;
width: 150px;
display: inline-bloxk;
}
Remove the <br> ?
echo ''.', '.''.$rows['lastname'].', '.$rows['firstname'].''.' = '.$rows['votes'];
Please help. Im trying to display the image of each user using their student ID
BUT it displays unknown characters as image on my page as below
%�S$P��H�~Q��-�}Y>ZTW�)�,G����g�~)�1WN�{�,��!���^j-����9�4���K�����
$sql_up="SELECT content FROM sp_uploads WHERE s_id= 'RC49557'";
$result_set=mysql_query($sql_up);
while($row_up = mysql_fetch_array($result_set))
{
$pp = $row_up['content'];
}
echo "<td>
<img style=\"float:right; margin:5px; width:150px; height:150px; padding:10px; \"
src=\" data:image/jpg;charset=utf8;base64,<?php echo $pp ?> \" /> </td>";
This will do. You don't need to open PHP tags inside PHP tags.
$sql_up="SELECT content FROM sp_uploads WHERE s_id= 'RC49557'";
$result_set=mysql_query($sql_up);
while($row_up = mysql_fetch_array($result_set))
{
$pp = $row_up['content'];
}
echo "<td>
<img style='float:right; margin:5px; width:150px; height:150px; padding:10px;'
src='data:image/jpg;charset=utf8;base64, $pp' /> </td>";
I'm assuming the img data is correct.
i am trying to find a way to get the tag from a string and remove the style attribute .
After that i want to add my own style and keep the following text..
For example, i have:
<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p>
end the endresult should be:
<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 100%;" /></p><p> </p><p>
I have unsuccesfully tried regex but it seems like i am too dumb to understand its functionality...
Every help would be appreciated!
Greetings from Greece
Jim
You can do it by regex like that:
$str = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p>';
$newStr = preg_replace('#<img (.+) style="(.+)" />#isU', '<img $1 style="width: 100%" />', $str);
To remove height or some other property:
$string = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p' ;
$pattern = "/height:\s*\d*\s*(px|%);*/" ;
$new = preg_replace($pattern,"", $string) ;
echo htmlentities($new) ;
Or remove all style things and replace with own ones:
$string = '<p><img alt="" src="images/epsth/arismagnisiakos.jpg" style="width: 600px; height: 405px;" /></p><p> </p><p' ;
$pattern = "/style=[\'\"][^\"|^\']*[\'\"]/" ;
$own_style = "style='width: 50%'" ;
$new = preg_replace($pattern, $own_style, $string) ;
echo htmlentities($new) ;
Generally using RegExp on HTML tags is kinda bad thing and should be avoided, but in some situations may be applicable.
From a variable containing tags layout as <img>
I want to replace all the image tags and content by the same tag concatenated with <br />
for example:
<img src="mon image" style="width:80px; float: right;">
replaced by
<img src="mon image" style="width:80px; float: right;"> <br />
I succed to replace the img tag with a character but not like I want
Try with this regular expression:
$string = '<img src="mon image" style="width:80px; float: right;">';
$pattern = "/(\<img\b[^>]*>)/";
$replacement = '${1}<br/>';
echo preg_replace($pattern, $replacement, $string);
Also #LawrenceCherone has right your IMG should be like this:
<img src="mon image" style="width:80px; float: right;"/>
But the regular expression also works with that tag declaration.
My string looking like the below:
Harvard researchers develop platinum-free solid-oxide fuel cells, which could be reliable and cheap enough for mobile technology.
<br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:4e19f3a5e1811fbd8d2609ab6e0b1790:xyHFPVLHAjBSu%2BHuriSZVqm9%2FODnAB81kZMY%2FW6XQhWC4ZbRzX%2BBHz7jOt1kjazUZT27efFh3vpwUMU%3D'><img border='0' title='Add to Twitter' alt='Add to Twitter' src='http://images.pheedo.com/images/mm/twitter.png'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:3e50e6b8d521eded6b35c7688aa906da:FrGGBNm1fSQsiuLmppzKM%2FATrKIoUDahb5X8uecXFxZVxeVzefUBbunDDSQIoM%2B7vZ%2FrMkI9MRbSJd0%3D'><img border='0' title='Add to Facebook' alt='Add to Facebook' src='http://images.pheedo.com/images/mm/facebook.gif'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:7e7e04c16c4c141c4117385690e52041:cBfF0Lt5lnF2klwL0yP1Z6C%2Bf6BV3FBNn1SMd9UUC1sTvBMcqqLi2LdjjD2Xx6LbCORRi%2F1sjoNWBYk%3D'><img border='0' title='Add to Slashdot' alt='Add to Slashdot' src='http://images.pheedo.com/images/mm/slashdot.png'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:abab99ee3bc19459ff3e8b7d8021840f:liR9O7Zfc0bI0Uuo10wyGIUoEOxlQXTWkWXuk6sb878dMYT2smVK1G5l0DxnIogEym5utExwYXrvUdM%3D'><img border='0' title='Add to digg' alt='Add to digg' src='http://images.pheedo.com/images/mm/digg_64x16.png'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:337479bf5b0ca164e90b9e6ee29a6bd2:Vo4lgAzFi7rA3OwEbIn3MCn8Wrc6ghW%2Bn8U%2FWvVnD%2FZAepXiRJLuKQ9jRNIB3tCaMfJBzkI0lN26WA%3D%3D'><img border='0' title='Add to del.icio.us' alt='Add to del.icio.us' src='http://images.pheedo.com/images/mm/delicious.gif'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:f1603669def1f797d29577bc6ffac6d8:L0FI9gIqTiSjo7LLh4IW%2FjEmU%2BevF%2Be%2B3Qh%2BEBIpZtBVoZeKf3mDbDWW%2FMjgIjP%2FujuheGGUDnffEyc%3D'><img border='0' title='Add to StumbleUpon' alt='Add to StumbleUpon' src='http://images.pheedo.com/images/mm/stumbleit.gif'/></a>
<a style='font-size: 10px; color: maroon;' href='http://www.pheedcontent.com/hostedMorselClick.php?hfmm=v3:6935f5f46c828b54e7f0a20ec855a0bc:31pmU19Ai%2BBD4P%2Bra8NUD3ywNzoeb%2B%2B%2F3UGXkY0dOTgERp7CGY8D%2FkhkfhSbDSkXYVynDgrcwPHJ3Q%3D%3D'><img border='0' title='Email this Article' alt='Email this Article' src='http://images.pheedo.com/images/mm/emailthis.png'/></a>
<br clear="both" style="clear: both;"/>
<img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=08f79e2459078baab633a35da651dfa4&p=1"/>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://segment-pixel.invitemedia.com/pixel?code=TechBiz
&partnerID=167&key=segment"/><img alt="" height="0" width="0" border="0" style="display:none" src="http://pixel.quantserve.com/pixel/p-8bUhLiluj0fAw.gif?labels=pub.29821.rss.TechBiz
.15217,cat.TechBiz
.rss"/>
From the above string i only want the first line of it, ie:"Harvard researchers develop platinum-free solid-oxide fuel cells, which could be reliable and cheap enough for mobile technology."
Rest all is not required.
To do so, I have tried:
$description = split(' \n', $string);
$description = split('<', $string);
$description = split('. ', $string);
But none of them gives me the required result, which would be only the first line, without and spaces or lines after the full-stop.
Can some one plz help me with this.
Also, if someone could edit my text, as i am unable to paste my example string, as it actually is.
Regards
Zeeshan
In regex the dot character is a special character, you would need to escape it :
$description = split('\.', $string);
Also, split seems to be deprecated, you should be using preg_split: http://us.php.net/manual/en/function.preg-split.php
Something like:
$description = current(explode("\n", $string));
should work fine. Pay special attention to the double instead of single quotes.
If you're doing more of this kind of parsing, you should consider a HTML parser, such as a simplehtmldom.