mraza Posted November 3, 2009 Share Posted November 3, 2009 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 More sharing options...
Mchl Posted November 3, 2009 Share Posted November 3, 2009 strip_tags can be told to leave <img> tag. Then you can use preg_replace to change it to bbcode Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950455 Share on other sites More sharing options...
mraza Posted November 3, 2009 Author Share Posted November 3, 2009 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 Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950466 Share on other sites More sharing options...
Mchl Posted November 3, 2009 Share Posted November 3, 2009 I moved this topic to regex section, where you're more likely to get help on this. Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950515 Share on other sites More sharing options...
thebadbad Posted November 3, 2009 Share Posted November 3, 2009 < 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. Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950568 Share on other sites More sharing options...
mraza Posted November 4, 2009 Author Share Posted November 4, 2009 Thanks but stil i am not getting the image instead a code like this: [img=http://image.info/200910/186336.jpg] border="0" alt="" /> Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950829 Share on other sites More sharing options...
mraza Posted November 4, 2009 Author Share Posted November 4, 2009 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 Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950832 Share on other sites More sharing options...
mraza Posted November 4, 2009 Author Share Posted November 4, 2009 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 Thanks for any help Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950881 Share on other sites More sharing options...
Mchl Posted November 4, 2009 Share Posted November 4, 2009 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); Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950897 Share on other sites More sharing options...
mraza Posted November 4, 2009 Author Share Posted November 4, 2009 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 ]. Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950910 Share on other sites More sharing options...
thebadbad Posted November 4, 2009 Share Posted November 4, 2009 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); ?> Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950915 Share on other sites More sharing options...
mraza Posted November 4, 2009 Author Share Posted November 4, 2009 ThANK YoU thebadbad ! you are genius , these tiny things you did in expressions are awesome, I am still reading about expressions and it is really a big help. thanks.. Link to comment https://forums.phpfreaks.com/topic/180175-solved-removing-html-tags-except-image/#findComment-950917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.