ayok Posted October 24, 2008 Share Posted October 24, 2008 Hi, I've been creating a news site which shows the news list on the front page with some parts of the news content. So I cut the news and people can click on "complete news" button to see the detail. My problem is when there are images inside the news which are also shown in the news list. What I have to do is to remove the img tag from the list so the image will not be shown. Recently I used str_replace to remove the "<img src=", "align=", etc. However I cannot remove the whole text inside <img>. My question is how can I remove those? I have tried this code as well <?php $cutNews = preg_replace('#(<[/]?img.*>)#U',$cutNews); ?> , but it doesn't work. It removes the whole news content. Anybody has a solution for me?? Thank you, ayok Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/ Share on other sites More sharing options...
r-it Posted October 24, 2008 Share Posted October 24, 2008 why dont you use strip_tags it works for me Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/#findComment-673549 Share on other sites More sharing options...
ayok Posted October 24, 2008 Author Share Posted October 24, 2008 Hi r-it, I've tried strip_tags, but it only removes the html tag and leaves the rest like src="blabla.jpg" align="left" etc which looks messy. ayok Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/#findComment-673552 Share on other sites More sharing options...
valtido Posted October 24, 2008 Share Posted October 24, 2008 ereg_replace("<img[^>]*>", "", $your_string) give this a go ... Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/#findComment-673583 Share on other sites More sharing options...
ayok Posted October 24, 2008 Author Share Posted October 24, 2008 ereg_replace("<img[^>]*>", "", $your_string) give this a go ... Thanks valtido, I've tried, but got the same result as preg_replace('#(<[/]?img.*>)#U',$cutNews); The other text is gone. ayok Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/#findComment-673612 Share on other sites More sharing options...
ayok Posted October 25, 2008 Author Share Posted October 25, 2008 Anyone? I need to remove that <img src="img.jpg" align="left" width="100" etc />. Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/#findComment-674671 Share on other sites More sharing options...
DarkWater Posted October 25, 2008 Share Posted October 25, 2008 Use: '/<img[^>]*>/i' Inside preg_replace() instead of the other regex. Quote Link to comment https://forums.phpfreaks.com/topic/129926-removing-img-tag/#findComment-674673 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.