Jump to content

[SOLVED] strip out image attributes using preg_replace


Recommended Posts

I'm trying to strip out all tags from an image tag leaving just the image source behind.

 

This is to be used on a dynamic stylesheet the following code is stored in a database:

 

<p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p>

 

Here's what I'm using:

 

<?php

// strip out everything but the image path I want the final out come to be: assets/editor/uploaded/bg.png

$src = "<p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p>";

$pattern[0] = "/\<p>(.*?)\<\/p\>/is";
$pattern[1] = "/\<img src=\"(.*?)\"/is";
$pattern[2] = "/\width=\"(.*?)\"/is";
$pattern[3] = "/\height=\"(.*?)\"/is";
$pattern[4] = "/\alt=\"(.*?)\"/is";
$replace[0] = "$1";
$replace[1] = "$1";
$replace[2] = "";
$replace[3] = "";
$replace[4] = "";
$output = preg_replace($pattern, $replace, $src);

//print result
echo $output;

// result is: <img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' />

?>

 

Can't seem to get it to work hope someone can point me in the right direction.

I'm not sure I follow.. you haven't provided a before AND after sample...(no offense..most people do not know how to accurately describe what they are looking for.. that is why it is always advisable to show a sample of what you start with [which you did], and what you want it to be afterwards..

 

If you can provide the 'after' part of the sample:

<p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p>

that would help out alot.

 

 

 

 

okay the after part I'd like it to get just the image path excluding any height,wide,alt attributes

 

like:

assets/editor/uploaded/bg.png

 

But when I run the code I get back

 

<img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' />

 

This will be apart of a dynamic stylesheet so I need the image path but none of the attributes.

Well, using preg_replace, (and assuming the image tag is always nested within the p tag):

 

$str = "<p><img src='assets/editor/uploaded/bg.png' alt='' width='15' height='15' /></p>";
$str = preg_replace('#<p>.+?src=[\'"]([^\'"]+)[\'"].+</p>#i', "$1", $str);
echo $str;

 

Output:

assets/editor/uploaded/bg.png

Excellent :)

 

EDIT - In hind sight.. best to use this version instead..

 

$str = preg_replace('#<p>.+?src=[\'"]([^\'"]+)[\'"].*</p>#i', "$1", $str);

 

Note the star in .*</p> instead of a +. This version would work in the event there is nothing between the src quotes and the closing p tag (which I assume would not be the case..but would still not hurt).

  • 10 months later...

sorry for bumping so old topic:

 

Sir @nrg_alpha about exactly something i am looking for but what if i don't have <p> tag around what code it should be thanks for the help.

 

 

Wow.. a thread nearing 1 year old, bumped back from the grave. So what you are looking for is to grab the contents of the src attribute? If you could provide a sample string and what exactly you are looking for, this would help out.

Seems you got your answer in that thread (which is solved). If not, then post the newer problem either in that thread (since it's much newer and more relevant to your problem) or start a new thread.

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.