PHP Help
- Started
- Last post
- 7 Responses
- effour
I need the following code to strip the height and width tag. Currently it's only stipping the width. What am I doing wrong with my php below, and how can i have it strip the height tag as well???
Thanks!
<?php preg_match_all('/<img([^>]*)>/i', $item->get_content(), $matches);
$all_images = $matches[0];
$string = $all_images[0];
$patterns = '/\width=".*?"/';
$replacements = 'width=""';
$newstring = preg_replace($patterns, $replacements, $string);
$patterns = '/\height=".*?"/';
$replacements = 'height=""';
$finalstring = preg_replace($patterns, $replacements, $newstring);
$finalstring = preg_replace('/src="(.*)"/U', 'src="http://www.mysite.com/timt... $finalstring);
echo $finalstring;
?>Outputs:
- drgs0
check if height tags use " og ' in the original code
- drgs0
or better else remove the backslashes in your patterns before w and h
"\w" matches any word character, thats why "?idth" is matched and striped, not because it actually matches "width"
- effour0
hmm. still no worky.
I guess the main question i have is...
This works:
$patterns = '/\width=".*?"/';
$replacements = 'width=""';
$newstring = preg_replace($patterns, $replacements, $string);This does not:
$patterns = '/\height=".*?"/';
$replacements = 'height=""';
$finalstring = preg_replace($patterns, $replacements, $newstring);They are the same code, but it only strips the width...why?
- comicsans0
In your $patterns= lines the /\ should be ^ (shift 6 on a UK keyboard), not sure ^ is even necessary but if you have typed the two characters / and \ then it will not work.
- spot130
The cube messes with code, you're better to post to http://pastebin.com/
- mikotondria30
Can't you just explode the image tag by " ", then rewrite in with the src as the 1th element ?
Like so:
http://pastebin.com/p4gAEuuk