Jump to content

[SOLVED] removing html tags except image


mraza

Recommended Posts

Hi team ! i am trying to remove html tags around my html file.

example code:

<div>
<p>This is an image </p>
<img src="http://image.info/200910/186336.jpg" border="0" alt="" /><br /><br />
</div>

when i use strip_tags it will remove everything and i will have only "This is an image" text left, is it possible that i can display the image link and it will sounded like this:

[img=http://image.info/200910/186336.jpg] 

 

so in my code it will display the paragraph with image below. Thanks for any support

Regards

 

Link to comment
https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/
Share on other sites

Thank you Sir ! i did this but now i am getting this, dont know where i went wrong:

 <[img] src="http://image.info/200910/186336.jpg" border="0" alt="" />

here is what i have done:

 
$content = str_ireplace($replacing, "*****", $content); 
$content =  strip_tags($content, '<img>');
$patterns = '<img>';
$replacement ='[img]';
$content =  preg_replace($patterns, $replacement, $content);

Thanks for support  :)

< and > function as pattern delimiters in your pattern <img>, thus only the literal img are replaced.

Probably doesn't make sense to you, but here's how you could do it:

 

<?php
$content = '<div>
<p>This is an image </p>
<img src="http://image.info/200910/186336.jpg" border="0" alt="" /><br /><br />
</div>';
$content = strip_tags($content, '<img>');
$content = preg_replace('~<img\b[^>]+\bsrc\s?=\s?([\'"])(.*?)\1~is', '[img=$2]', $content);
?>

 

Just ask if you need something explained, and I'm sure a kind soul (if not me ;)) will help you understand.

hi i tried but regex is still hard for me, lol PHP is so deep when i started i thougt i will learn all quickly but now regex  ::) , Ok now what i did is as i was not able to remove the extra properties at image tag, i wanted to keep the <b> tag also so i did this code:

<?php
$content = '<div>
<p>This is an image </p>
<img src="http://image.info/200910/186336.jpg" border="0" alt="" /><br /><br />
</div>';
$content = strip_tags($content, '<img><b>');
$content = preg_replace('~<img\b[^>]+\bsrc\s?=\s?([\'"])(.*?)\1~is', '[img=$2]', $content);
$replace = '<b>';
$with = '[b]';
$content = preg_replace($replace , $with, $content);
?>

when i run this code every b word on the page was surrounded like [ b ]this  :D

Thanks for any help

That's because preg_replace thinks < and > are delimiters for your regex pattern and not part of patter itself.

Try like this:

$content = preg_replace('#'.$replace.'#' , $with, $content);

Thanks Sir that did the trick i dont know if it was good but i also remove the border and alt tage this way:

$content = strip_tags($content, '<img><b>');
$content = preg_replace('~<img\b[^>]+\bsrc\s?=\s?([\'"])(.*?)\1~is', '', $content);
$replace = '<b>';
$with = '[b]';
$content = preg_replace('#'.$replace.'#' , $with, $content);
$replace2 = '</b>';
$with2 = '[/b]';
$content = preg_replace('#'.$replace2.'#' , $with2, $content);
$replace3 = 'border="0" alt="" />';
$with3 = '';
$content = preg_replace('#'.$replace3.'#' , $with3, $content);

 

what i was thinking is what if i have something else on 'border="0" alt="" />' is that not possible that i can make it, it will remove everything in the image tag and just extract the link and tag between [ img ].

ah sorry my mistake i had turned off bbc code, now i am getting exactly the picture , thanks but still i am getting  border="0" alt="" /> how can i remove that. THanks

 

Sorry, forgot the rest of the expression :)

And I just realized that there's no need to run strip_tags() with the second parameter before translating the tags in question to BBCode. Updated code:

 

<?php
$content = '<div>
<p>This is an image </p>
<img src="http://image.info/200910/186336.jpg" border="0" alt="" /><br /><br />
</div>';
$replace = array(
'~<img\b[^>]+\bsrc\s?=\s?([\'"])(.*?)\1[^>]*>~is' => '[img=$2]',
'~<b\b[^>]*>(.*?)</b>~is' => '[b]$1[/b]'
);
$content = preg_replace(array_keys($replace), $replace, $content);
$content = strip_tags($content);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.