fc123 Posted March 21, 2008 Share Posted March 21, 2008 I have used regex to remove links from user submitted content in the following function: function linkremove($text) { $text = preg_replace("#www\.(.+?)\.com#is", "", $text); $text = preg_replace("#www\.(.+?)\.net#is", "", $text); $text = preg_replace("#www\.(.+?)\.co.uk#is", "", $text); $text = preg_replace("#www\.(.+?)\.org#is", "", $text); return $text; } Although not the most difficult to get around it seems somewhat effective at this moment in time. However, sometimes users also use my BBCode to insert pictures such as: [ img ] www.p1.examplesite.com/folder/pic.jpg [ / img ] or [ img ] www.examplesite.com/folder/pic.jpg [ / img ] which when submitted strips out the www.p1.examplesite.com / www.examplesite.com Is there a way I can add a line to my code above that can identify SAFE sites that I specify and ignore or leave these these, such as photobucket so that the image can be shown? I hope I explained that well... Link to comment https://forums.phpfreaks.com/topic/97232-user-content-removal/ Share on other sites More sharing options...
veridicus Posted March 21, 2008 Share Posted March 21, 2008 I would keep an array of safe sites and use preg_match instead of preg_replace. Then loop through the matches and str_replace those that are not safe. Link to comment https://forums.phpfreaks.com/topic/97232-user-content-removal/#findComment-497530 Share on other sites More sharing options...
fc123 Posted March 21, 2008 Author Share Posted March 21, 2008 Sounds like an idea, never used preg_match before though. Before I go away and look it up, could you give an example? Link to comment https://forums.phpfreaks.com/topic/97232-user-content-removal/#findComment-497533 Share on other sites More sharing options...
lordfrikk Posted March 21, 2008 Share Posted March 21, 2008 <?php function linkremove($text) { if (!preg_match('#\.(jpe?g|gif|png|bmp)$#is'), $text): $text = preg_replace("#www\.(.+?)\.com#is", "", $text); $text = preg_replace("#www\.(.+?)\.net#is", "", $text); $text = preg_replace("#www\.(.+?)\.co.uk#is", "", $text); $text = preg_replace("#www\.(.+?)\.org#is", "", $text); endif; return $text; } ?> Link to comment https://forums.phpfreaks.com/topic/97232-user-content-removal/#findComment-497535 Share on other sites More sharing options...
lordfrikk Posted March 21, 2008 Share Posted March 21, 2008 I'm sorry I realized the above script is totally useless, because you're not testing single links but whole text. You need to use preg_match to capture all links to an array, then loop through it and use the if (!preg_match('#\.(jpe?g|gif|png|bmp)$#is'), $text) condition to identify image links and let them through. I'll write it down if you won't, but right now I'm off to pub Link to comment https://forums.phpfreaks.com/topic/97232-user-content-removal/#findComment-497562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.