Chevy Posted December 16, 2007 Share Posted December 16, 2007 Okay, I have a bbCode function on my site... function bbCode($input){ $input = strip_tags($input); $bbcodes = array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[img=',']'); $htmlbbcodes = array ('<b>','</b>','<i>','</i>','<u>','</u>','<img src="','" />'); $input = str_replace($bbcodes, $htmlbbcodes, $input); return $input; } So I am wondering, if the enter their string which can be like "Blah aha blah IMAGE bla bal blah blah blah" How can I get the image URL out of that and check the size of it in my bbCode function? Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 16, 2007 Author Share Posted December 16, 2007 Any solutions? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 16, 2007 Share Posted December 16, 2007 Is this sort of what you want? <?php function handle_img_bbcode($matches) { $url = $matches[1]; list($width, $height) = getimagesize($url); return "<img src='{$url}' width='{$width}' height='{$height}' />"; } $string = "[img=http://www.phpfreaks.com/images/logo_main.jpg]"; $string = preg_replace_callback("`\[img\]([\w]*[:\/\/]*[\w\.\?\/&=\;, \-@%\?]+)\[/img\]`isU", 'handle_img_bbcode', $string); echo $string; ?> Output: <img src='http://www.phpfreaks.com/images/logo_main.jpg' width='510' height='94' /> I don't see why you'd want to get the height and width for bbcode images. If there are more than a few then it might increase the load time as the server first would have to download the image (and wait until it's done) and then the user would have to download the image as well. Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 16, 2007 Author Share Posted December 16, 2007 Well you see I don't want images being over 400px because the images are used on the forums and if they are to big it stretches the boards and stuff... I just want it to display an error if the image height is over 600px and the width is over 450px... But you see I don't know how to get the image from a string... The string could look like this: Hey everyone I just wanted you guys to see my new pic. [img=imageurl] So you know, comment me back or something Does that make it more clear? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 16, 2007 Share Posted December 16, 2007 Yeah, the above code I posted will do that (get the image url+width+height). If there is anything in the code you don't understand feel free to ask Just modify handle_img_bbcode() to throw an error if it's too big. Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 16, 2007 Author Share Posted December 16, 2007 Yea I have a question; how would I make it work in this function? function bbCode($input){ $input = strip_tags($input); $bbcodes = array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[img=',']'); $htmlbbcodes = array ('<b>','</b>','<i>','</i>','<u>','</u>','<img src="','" />'); $input = str_replace($bbcodes, $htmlbbcodes, $input); return $input; } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 16, 2007 Share Posted December 16, 2007 You could do it like this: <?php function handle_img_bbcode($matches) { $url = $matches[1]; list($width, $height) = getimagesize($url); // check if it's too large here... return "<img src='{$url}' width='{$width}' height='{$height}' />"; } function bbCode($input) { $input = strip_tags($input); $bbcodes = array('[b]','[/b]','[i]','[/i]','[u]','[/u]'); $htmlbbcodes = array('<b>','</b>','<i>','</i>','<u>','</u>'); $input = preg_replace_callback("`\[img\]([\w]*[:\/\/]*[\w\.\?\/&=\;, \-@%\?]+)\[/img\]`isU", 'handle_img_bbcode', $input); $input = str_replace($bbcodes, $htmlbbcodes, $input); return $input; } ?> Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 17, 2007 Author Share Posted December 17, 2007 So my varible is like this: $message = bbCode(filter($_POST['message'])); It would have to be $message = handle_img_bbcode(bbCode(filter($_POST['message']))); Correct? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 17, 2007 Share Posted December 17, 2007 No, handle_img_bbcode() is what is a callback function. Basically preg_replace_callback() will pass an array of matches to that function. Take a look at http://php.net/preg_replace_callback for more info. You just need to pass it to the bbCode() function. Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 17, 2007 Author Share Posted December 17, 2007 Oh, I see it xD Thanks a ton, I will try it now... Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 17, 2007 Author Share Posted December 17, 2007 I did this: function handle_img_bbcode($matches){ $url = $matches[1]; list($width, $height) = getimagesize($url); if ($width > 450){ error("Image To Large!", "Your image is over 450 pixels wide.", ""); } if ($height > 600){ error("Image To Large!", "Your image is over 600 pixels tall.", ""); } } And it does not work... I also added: $input = preg_replace_callback("`\[img\]([\w]*[:\/\/]*[\w\.\?\/&=\;, \-@%\?]+)\[/img\]`isU", 'handle_img_bbcode', $input); To my bbCode function. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 17, 2007 Share Posted December 17, 2007 handle_img_bbcode() still needs to return something as that is what the tag will be replaced with. If you add in the return from my original function at the end if handle_img_bbcode() then it should work again. Quote Link to comment Share on other sites More sharing options...
Chevy Posted December 17, 2007 Author Share Posted December 17, 2007 Ah, okay Got it to work! Thanks a ton again Quote Link to comment 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.