Jump to content

Recommended Posts

hi there,

 

i want to add the width attribute if there is not or change the width value to an img tag that comes from the content.

 

for example, i have the following content

 

<p>Also from the wonderful Tank Theory line is this nice Comrade t-shirt. She’s not all that innocent, not that she really looks it, but if you inspect the shirt closer you’ll see she is hiding something on the back. The shirt is now available in red with black wterbased print, and gold foil accents.</p>
<p><a title="Tank Theory Comrade" href="http://www.tanktheory.com/store/standard-t-shirts/product/comrade/" target="_blank"><img class="alignnone size-full wp-image-2642" title="m_509_tank_theory_red" src="http://www.birdfight.com/blogfiles/wp-content/uploads/2009/02/m_509_tank_theory_red.jpg" alt="m_509_tank_theory_red" height="400" width="399"></a></p>

 

or how to resize the image dynamically

 

thanks for you help :)

With the image tag:

 

<?php
$string = '<p>Also from the wonderful Tank Theory line is this nice Comrade t-shirt. She’s not all that innocent, not that she really looks it, but if you inspect the shirt closer you’ll see she is hiding something on the back. The shirt is now available in red with black wterbased print, and gold foil accents.</p>
<p><a title="Tank Theory Comrade" href="http://www.tanktheory.com/store/standard-t-shirts/product/comrade/" target="_blank"><img class="alignnone size-full wp-image-2642" title="m_509_tank_theory_red" src="http://www.birdfight.com/blogfiles/wp-content/uploads/2009/02/m_509_tank_theory_red.jpg" alt="m_509_tank_theory_red" height="400" width="399"></a></p>';

$string = '<p>testing this other image <img src="blahblah.jpg" height="50">';

if (stristr($string, "<img") !== false) {
if (preg_match('~<img.*width="(.*)".+?>~is', $string) > 0) {		
	$string = preg_replace("~<img(.*)width=\"(.*)\"(.+?)>~is", "<img$1width=\"500\"$3>", $string);
}else {		
	$string = preg_replace("~<img(.+?)>~is", "<img $1 width=\"500\">", $string);
}
}
echo htmlentities($string);
?>

 

Tested for both (commented out the lower one to test to upper). It worked as expected. As far as if there is a better way to do this, no clue, probably since I am still new to regex.

 

I am working on a better way to do it :)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.