Clarkeez Posted August 4, 2011 Share Posted August 4, 2011 Hi, I'm in the process of making a forum system for my clan website. I'm currently making a function that convert bbcode tags to html tags ready for echoing in the div. Additionally, it will check all the images posted in forum post to see if they are over a certain width, and if they are, to apply a 'width=xxx' attribute to the img tag. Here is the working function I have. function bbcode($input) { // changes all below bbcode to their repsective html partners as html is blocked in the web forms $start_arr = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[img=', ']', ' [center]', '[/center] '); $finish_arr = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<img src="', '"/>', '<center>', '</center>'); $replaced = str_replace($start_arr, $finish_arr, $input); // does the input have a URL within image tags? $matchcount = preg_match('!<img (?:.*?)src=([\'"])(http://[^ ]+)\\1(/){0,1}>!i', $replaced, $matches); if($matchcount >= 1) { // yes $urlWithtags = $matches[2]; $urlWithtags = str_replace('<img src="', '', $urlWithtags); // strip off <img src=" $urlNotags = str_replace('"/>', '', $urlWithtags); // strip off "/> we now have the raw URL of image $imgsize = getimagesize($urlNotags); // Check if image is too wide if($imgsize[0] > 777) { // too wide $doneString = str_replace($urlNotags, ''.$urlNotags.'" width="777', $replaced); return $doneString; // send back the new imgtag } else { // not too wide, dont add size limit $doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced); return $doneString; // send back the new imgtag } } else { // no return $replaced; // send back original string, no image was found } } Now, here is the part we need to focus on $imgsize = getimagesize($urlNotags); // Check if image is too wide if($imgsize[0] > 777) { // too wide $doneString = str_replace($urlNotags, ''.$urlNotags.'" width="777', $replaced); return $doneString; // send back the new imgtag } else { // not too wide, dont add size limit $doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced); return $doneString; // send back the new imgtag } It works absolutely fine, but will only resize the FIRST image posted in the post. So, if someone posted 2 images that were too wide, then it would only resize the first one. I'm not sure how to go about making this script check all the img tags in the post to check if they are too big.. Can anyone help, I really need it Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/ Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 edit: nevermind I see what you did here. Well your only grabbing 1 match from the preg_match, you need to count the number of elements inside $matches and do a for loop. $matchcount = preg_match('!<img (?:.*?)src=([\'"])(http://[^ ]+)\\1(/){0,1}>!i', $replaced, $matches); $num_matches = count($matches); for($i=0;$i<=$num_matches;$i++){ } Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252167 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 see my post above. Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252170 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Yeah I was thinking str_replace should work fine because it managed to change the bbcode to html tags fine I just cant figure this out Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252171 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Well your only grabbing 1 match from the preg_match, you need to count the number of elements inside $matches and do a for loop. Thanks for the hint phpSensei, Im quite moderate with php and wouldn't know how to go about this properly. Could you hint further ? Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252172 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 See post above. Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252173 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Ok let me try that, I was going to suggest some sort of count method, brb Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252174 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Ok so its still only resizing first image <?php $matchcount = preg_match('!<img (?:.*?)src=([\'"])(http://[^ ]+)\\1(/){0,1}>!i', $replaced, $matches); $num_matches = count($matches); for($i=0;$i<=$num_matches;$i++) { $urlWithtags = $matches[2]; $urlWithtags = str_replace('<img src="', '', $urlWithtags); // strip off <img src=" $urlNotags = str_replace('"/>', '', $urlWithtags); // strip off "/> we now have the raw URL of image $imgsize = getimagesize($urlNotags); // Check if image is too wide if($imgsize[0] > 777) { // too wide $doneString = str_replace($urlNotags, ''.$urlNotags.'" width="777', $replaced); return $doneString; // send back the new imgtag } else { // not too wide, dont add size limit $doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced); return $doneString; // send back the new imgtag } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252179 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 thats because $matches[2]; has to be $matches[$i][2]; it has to loop through the matches. Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252184 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks for the reply again however its gone completly bonkers now, its outputting this <b>Warning</b>: getimagesize(m) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: No such file or directory in <b>/home/almosti1/public_html/beta/inc/func/bbcode.php</b> on line <b>21</b><br /> <div class="post_box"><b>Hey<b><br /> <i>Testing auto<img src="m"/>atic i<img src="m"/>age resizing..</i><br /> <i<img src="m"/>g src="http://www.yorapper.co<img src="m"/>/Photos/big-boi-ringtones.jpg"/><br /> <br /> <i<img src="m"/>g src="http://www.clash<img src="m"/>usic.co<img src="m"/>/files/i<img src="m"/>agecache/big_node_view/files/TheNotoriousBIG_1.jpg"/><br /> <br /> <i<img src="m"/>g src="http://www.wrestlescoop.co<img src="m"/>/wallpaper/big_show_wallpaper_02.jpg"/><br /> <br /> <i<img src="m"/>g src="http://i<img src="m"/>g.daily<img src="m"/>ail.co.uk/i/pix/2008/01_02/InTheBathRUK_468x324.jpg"/></div><br /> <b>Warning</b>: getimagesize() [<a href='function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in <b>/home/almosti1/public_html/beta/inc/func/bbcode.php</b> on line <b>21</b><br /> for some unknown reason it seems to be taking all the 'm''s out of the text and putting img tags around them. When I change $matches[$i][2] back to $matches[2] it works again, but only first image is resized. weird. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252189 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 can you post your new code? Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252197 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Sure see attached its always better to see it in dreamweaver or notepad++ or whatever [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252203 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 I'll test and fix it on my server, gimme a minute. Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252209 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Sorry I forgot about you haha Updated code, now finish it The $images = $matches[0][$i]; will return "<img src='asdsad.jpg' />" or w.e, now you can use your code. <?php // require('resize.php'); error_reporting(E_ALL ^ E_NOTICE); function bbcode($input) { // changes all below bbcode to their repsective html partners as html is blocked in the web forms $start_arr = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[img=', ']', ' [center]', '[/center] '); $finish_arr = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<img src="', '"/>', '<center>', '</center>'); $replaced = str_replace($start_arr, $finish_arr, $input); // does the input have a URL within image tags? $matchcount = preg_match_all('/<img[^>]+>/i',$replaced, $matches); $urlNotags =""; $num_matches = count($matches[0]); for($i=0;$i<=$num_matches;$i++) { $images = $matches[0][$i]; // } } Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252221 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks again for this but Still not working, still only changing the first one. This is deffo how I'm meant to use it? <?php // require('resize.php'); function bbcode($input) { // changes all below bbcode to their repsective html partners as html is blocked in the web forms $start_arr = array('[b]', '[/b]', '[u]', '[/u]', '[i]', '[/i]', '[img=', ']', ' [center]', '[/center] '); $finish_arr = array('<b>', '</b>', '<u>', '</u>', '<i>', '</i>', '<img src="', '"/>', '<center>', '</center>'); $replaced = str_replace($start_arr, $finish_arr, $input); // does the input have a URL within image tags? $matchcount = preg_match_all('/<img[^>]+>/i',$replaced, $matches); $urlNotags =""; $num_matches = count($matches[0]); for($i=0;$i<=$num_matches;$i++) { $images = $matches[0][$i]; // $urlWithtags = $images; $urlWithtags = str_replace('<img src="', '', $urlWithtags); // strip off <img src=" $urlNotags = str_replace('"/>', '', $urlWithtags); // strip off "/> we now have the raw URL of image $imgsize = getimagesize($urlNotags); // Check if image is too wide if($imgsize[0] > 777) { // too wide $doneString = str_replace($urlNotags, ''.$urlNotags.'" width="777', $replaced); return $doneString; // send back the new imgtag } else { // not too wide, dont add size limit $doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced); return $doneString; // send back the new imgtag } } } ?> So you can test, there is 4 different images urls wrapped in [/img] tags that are inputted from the DB into this function. The first and third images are too big and should be resized to 777px wide. The second and forth images are under 777px and nothing should be changed. Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252229 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Your supplying this variable with an undefined variable. Its not suposed to be urlWithtags = $images; $urlWithtags = str_replace('<img src="', '', $urlWithtags); // strip off <img src=" its suposed to be urlWithtags = $images; $urlWithtags = str_replace('<img src="', '', $images); // strip off <img src=" Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252237 Share on other sites More sharing options...
Clarkeez Posted August 4, 2011 Author Share Posted August 4, 2011 Your going to hate me when I say that still doesn't work :-\ Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252240 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 No I expected to hear that, because the rest of you code. In my opinion, you need stop using the str_replace for stripping off each and every pieces of the $images variable, and do preg_relace. Don't cut pieces of and put them back together, you can easily just do it with regex. This is what I would do. I would take the $images variable which contains the img tag, and then I would get the "width" attribute with regex, I would see if the returned width too large, if it is, then do a str_replace for the width, and return the $images, otherwise i would leave it be. Instead of doing str_replace("<img src-'".. Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252243 Share on other sites More sharing options...
Clarkeez Posted August 5, 2011 Author Share Posted August 5, 2011 Do you have any nice websites that I can use on how to learn regex? Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252259 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Just google things like "Preg_match img src" Quote Link to comment https://forums.phpfreaks.com/topic/243861-repeating-process-in-a-function/#findComment-1252264 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.