ecopetition Posted November 11, 2008 Share Posted November 11, 2008 Hi there, I'm trying to control what image extensions a user can put into an image BBCode, which works with preg_replace. Basically, I don't want them to put .php, .js, .html etc extensions into tags, so need it return an error when they try to do this. However, I do want them to put .png, .bmp, .gif, .jpg etc into the code. Also, if I was to type the following: [img=www.hotmail.com] As well as: [img=www.hotmail.com/image.php.gif] How could I have this return an error also? (In other words, only if it contains a proper image extension at the end should it work). Any advice as to how to go about this would be greatly appreciated. Thanks a lot in advance, Peter Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/ Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 Can I see your current img tag regex? Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-687935 Share on other sites More sharing options...
ecopetition Posted November 11, 2008 Author Share Posted November 11, 2008 Right now it's a rather not-very-sophisticated: $text = preg_replace('#\[img\](.+?)\[\/img\]#i', '<img src="$1" alt="" />', $text); (please note that I updated the first post). Thank you. Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-687938 Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 K, read the edit. So basically: $text = preg_replace('#\[img\]([^.]+\.(gif|jpeg|jpg|png))\[/img\]#', '<img src="$1" alt="" />', $text); EDIT: Small typo; had the wrong quotation mark. Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-687940 Share on other sites More sharing options...
wildteen88 Posted November 11, 2008 Share Posted November 11, 2008 A long winded version: $text = 'Hello World! Good: [img=http://www.google.co.uk/images/firefox/sprite.png] Bad: [img=http://www.google.co.uk/images/firefox/sprite.php]'; $text = preg_replace_callback('#\[img\](.+?)\[/img\]#s', 'parse_image', $text); function parse_image($data) { $trusted_ext = array('gif', 'jpg', 'jpeg', 'bmp', 'png'); $info = pathinfo($data[1]); if(in_array($info['extension'], $trusted_ext)) { return '<img src="'.$data[1].'" />'; } // this could be just "return false;" return '<span style="color:red"><b>REMOVED</b></span>'; } echo nl2br($text); Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-687949 Share on other sites More sharing options...
ecopetition Posted November 11, 2008 Author Share Posted November 11, 2008 K, read the edit. So basically: $text = preg_replace('#\[img\]([^.]+\.(gif|jpeg|jpg|png))\[/img\]#', '<img src="$1" alt="" />', $text); EDIT: Small typo; had the wrong quotation mark. I tried this method in place of the original code but it doesn't work - BBCode does not function with it. Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-687988 Share on other sites More sharing options...
DarkWater Posted November 11, 2008 Share Posted November 11, 2008 Worked for me. o_O Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-687991 Share on other sites More sharing options...
ecopetition Posted November 12, 2008 Author Share Posted November 12, 2008 DarkWaters code only worked for me when the file was a local one, and did not contain a path to the file. Any advice Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-688712 Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 Oh, good point. I see what happened. Try: $text = preg_replace('#\[img\](.+?\.(gif|jpeg|jpg|png))\[/img\]#', '<img src="$1" alt="" />', $text); Link to comment https://forums.phpfreaks.com/topic/132320-image-extensions-in-bbcode/#findComment-688829 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.