Dear all,
I’m having problem on removing width and height from <img src=“” width=“xx” height=“yyy” alt=“”> .
I tried with the eregi_replace as below:
1st try:
<?php $description= eregi_replace( “height=\”(.*)\“”, “”,$description); ?>
2nd try:
<?php $description= eregi_replace( “height=\”[^]*\“”, “”,$description); ?>
Do you see my error?
where is $description getting it’s data? mind posting it…
Oh, the $description is pulled from database…
Thus the format of the <img src> will be different according to how the user enter the data…
it might be:
Below is my photo<br><img src=“photo.jpg” width=“10” height=“10”><br>
or
Below is my photo<br><img width=“10” src=“photo.jpg” height=“15”><br>
or
Below is my photo<br><img width=“20” height=“20” src=“photo.jpg”><br>
4th & 5th try, also not working for these:
<?php $description= eregi_replace( “width=\”([0-9][0-9]?)\“”, “”,$description); ?>
<?php $description= preg_replace( “/[height=\”].+?[\“]/is”, “”,$description); ?>
PHPycho
November 17, 2009, 7:47am
5
Try this:
<?php
$string = '<img src="" width="xx" height="yyy" alt="">';
$new_string = preg_replace('/\\<(.*?)(width="(.*?)")(.*?)(height="(.*?)")(.*?)\\>/i', '<$1$4$7>', $string);
echo $new_string;
?>
Note this code only works for '<…width=“xx”…height=“yyy”…> format,
if width & height are interchanged(i.e. their occurance) then it doesn’t work.
Can anbody mody the above regular expression to suit the interchangeability?
Thanks
PHPycho
November 17, 2009, 9:22am
7
AnthonySterling:
NVM, didn’t work.
It’s working dude.
It prints:
<img src="" alt="">
Much better.
<?php
$html = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<img src="path/to/img" height="455" width="455" alt="" />
</body>
</html>
';
$doc = DOMDocument::loadHTML($html);
foreach($doc->getElementsByTagName('img') as $image){
foreach(array('width', 'height') as $attribute_to_remove){
if($image->hasAttribute($attribute_to_remove)){
$image->removeAttribute($attribute_to_remove);
}
}
}
echo $doc->saveHTML();
?>
PHPycho & anthonysterling,
Wow~ Expert solution!
Thanks for you guys sharing!
system
November 18, 2009, 4:02am
11
Outstanding work SilverBullet
PHPycho
November 18, 2009, 4:04am
12
PHPycho:
Try this:
<?php
$string = '<img src="" width="xx" height="yyy" alt="">';
$new_string = preg_replace('/\\<(.*?)(width="(.*?)")(.*?)(height="(.*?)")(.*?)\\>/i', '<$1$4$7>', $string);
echo $new_string;
?>
Note this code only works for '<…width=“xx”…height=“yyy”…> format,
if width & height are interchanged(i.e. their occurance) then it doesn’t work.
Can anbody mody the above regular expression to suit the interchangeability?
Thanks
Read my requirements in the quote. Do anybody has idea how to accomplish this using regular expression?
Hi, i found something like this:
preg_match_all(‘/<img[^>]+>/i’,$html, $result);
print_r($result);
$img = array();
foreach( $result as $img_tag)
{
preg_match_all(‘/(alt|title|src)=(“[^”]*")/i’,$img_tag, $img[$img_tag]);
}
print_r($img);
It splits the process in two parts :
* get all the img tag
* extract their metadata
Will this solve the problem???
http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php
joebert
November 30, 2009, 4:59pm
14
In your pattern, replace the words “width” and “height” with the following.
(?:width|height)
Don’t modify anything else, just swap both words out with that.