sillysillysilly Posted March 31, 2011 Share Posted March 31, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 31, 2011 Share Posted March 31, 2011 You do realize that an anchor tag (i.e. the <A> tag for links) is NOT an image tag, right? So, stripping out "<a href" would not remove images (e.g. "<img src=") Quote Link to comment Share on other sites More sharing options...
sillysillysilly Posted March 31, 2011 Author Share Posted March 31, 2011 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/'; Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 31, 2011 Share Posted March 31, 2011 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.