0

$image variable gives this code (when echo is used):

<img src="/image.png" width="100" height="147" alt="" class="some_class" />
  • width, height, src and class attributes can be different.

What should we do:

  1. remove width and height from $image
  2. replace alt="" with alt="Poster"

Thanks.

6
  • Are the widht/height incorrect or why do you want to remove them? Commented Jul 4, 2010 at 17:43
  • Why do you have markup stored inside a variable? Commented Jul 4, 2010 at 17:44
  • They break your JS code? How so? Commented Jul 4, 2010 at 17:44
  • Too much questions, anyone can just give a right code? Commented Jul 4, 2010 at 17:48
  • @Happy: Doubtful, it's not proper to remove parts of HTML in PHP, only insert things into it. Why don't you find the HTML and remove the width and height attributes and set an ALT? You don't need PHP to do that for you. Commented Jul 4, 2010 at 17:51

4 Answers 4

1

I'd use regular expressions to do this.

// Replace width="..." and height="..." with nothing
$variable = preg_replace('/(width|height)\s*=\s*"[^"]*"/i', '', $image);

// Replace alt="..." with alt="Poster"
$variable = preg_replace('/alt\s*=\s*"[^"]*"/i', 'alt="Poster"', $variable);

This method is not dependant on the order of the attributes, which of the some other answers are. Also note that the second regular expressions will replace alt="<any string here>" with alt="Poster". It should be easy enough to change it to only replace alt="" though.

Sign up to request clarification or add additional context in comments.

Comments

1
$pattern = '/alt="" width="\d+" height="\d+"/i';
$replacement = 'alt="Poster"';
$variable = preg_replace($pattern, $replacement, $variable);

fix to work with any order of attributes(Gordon's comment):

$pattern = '/alt=""/i';
$replacement = 'alt="Poster"';
$variable = preg_replace($pattern, $replacement, $variable);
$pattern = '/width="\d+"/i';
$replacement = '';
$variable = preg_replace($pattern, $replacement, $variable);
$pattern = '/height="\d+"/i';
$replacement = '';
$variable = preg_replace($pattern, $replacement, $variable);

2 Comments

this assumes the attributes will always appear in that order.
what if the attributes appear with more whitespace inbetween e.g. alt = "" width= "100" height= "147"?
1

If the attributes can be variable, then your best choice for working with the HTML is to use a library that actually understands HTML, like DOM.

$dom = new DOMDocument;
$dom->loadXML($variable);
$dom->documentElement->removeAttribute('width');
$dom->documentElement->removeAttribute('height');
$dom->documentElement->setAttribute('alt', 'poster');
echo $dom->saveXML($dom->documentElement);

outputs:

<img src="http://site.com/image.png" alt="poster"/>

2 Comments

Thanks Gordon! 1) with and height can be different. 2) replacement for alt doesn't work
I'd suggest 'width="100" height="147" alt=""' since there are going to be 2 alt's after your solution.
0

will also replace <embed width="x"> which is wrong. We need to catch only with <img> tag

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.