Jump to content

preg_replace for just one tag


sillysillysilly

Recommended Posts

I am trying to pull out the image HTML tag but its not working as expected.

Here is what I am doing

$removeimagetag='/<a href/';

$replacwith = "illegal";

$_POST['descr']=preg_replace($removeimagetag,$replacwith,$_POST['descr']);

 

in the 'descr' there is a <a href="http://s635.photobucket.com......

When it post it does not strip out the <a href

So the image is displayed.

 

I tried a modified version to test to see if it work that looked like this.

 

$removeimagetag='/goofy/';

$replacwith = "illegal";

$_POST['descr']=preg_replace($removeimagetag,$replacwith,$_POST['descr']);

 

When the word goofy appears in the text then its replaced with the word illegal,

 

Also I would like to make it so that it replaces the entire line from the <a href....... to the </a> with a phrase that says "this feature is not allowed"  rather than just the part in the front...the <a href.

 

Any help you can be will be greatly appreciated.

 

Link to comment
Share on other sites

Yes, I was keying off that to start checking since I want to remove links and images and most images from photoshop start with that.  the second test would be for the image itself then take that out.  So I guess I could have been more accurate in saying I wanted to remove the anchor link first.  I figured if I could get the link removal to work then I could get the image removal to work as well.  Any ideas.  I am certain its in my syntax of the $removeimagetag='/<a href/';

Link to comment
Share on other sites

No, your pattern will work, although it is only going to replace the beginning of the tag as you stated. That is why I questioned what you were really trying to do. If you were trying to remove images and your patten was looking for anchor tags, that would explain why it wasn't working.

 

Try the following:

$input = '
Before link text <a href="http://s635.photobucket.com">Link Name</a> after link text
Before image text <img src="theimage.jpg"> after image text';

$patterns = array(
    'links' => '/<a[^>]*>.*?<\/a>/',
    'images' => '/<img[^>]*>/'
    );
$replacement = "[illegal]";

$output = preg_replace($patterns, $replacement, $input);

?>
<pre>
<b>Input:</b><br />
<?php echo $input; ?>
<br /><br />
<b>Ouput:</b><br />
<?php echo $output; ?>
</pre>

 

Link to comment
Share on other sites

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.