0

I have a image with class 'attachment-fullslideshow'. The code is

<img class="attachment-fullslideshow" src="demo.jpg">

I would like to replace the class from 'attachment-fullslideshow' to 'attachment-fullslideshow quote'.

Please suggest.

4
  • do you want to do in php or jquery? Commented Jan 9, 2014 at 14:09
  • Isn't it better to use jQuery ? Commented Jan 9, 2014 at 14:10
  • It's better to use Jquery like $(".attachment-fullslideshow").addClass("quote"); Commented Jan 9, 2014 at 14:12
  • I know jquery will be better but I have to use php. Commented Jan 9, 2014 at 14:12

2 Answers 2

2

You could do something like this:

$str = '<img class="attachment-fullslideshow" src="demo.jpg">';

$str = str_replace('class="attachment-fullslideshow', 'class="attachment-fullslideshow quote', $str);

//Result: <div class="attachment-fullslideshow quote">...</div>

Or with regular expressions:

$str = '<img class="attachment-fullslideshow" src="demo.jpg">';
$str = preg_replace(':class="(.*attachment-fullslideshow.*)":', 'class="\1 quote"', $str);
//Result: <div class="defaultClass myClass">...</div>

Hope this helps!

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

Comments

1

You could also

$html = '<img class="attachment-fullslideshow" src="demo.jpg">';
$test = preg_replace('/class="(.*?)"/s', 'class="newclass"', $html);
echo $test;

outputs

<img class="newclass" src="demo.jpg">

1 Comment

Thanks David. It helps a lot.

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.