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]', '[/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 :)

Where is the loop ?
This routine on its own will not iterate through any list of images.
Do you call it for every image - if so, how ? I'm guessing you should look there.

~NZS.

// This is not 'working code' only an example of what you need to do.
		if($matchcount >= 1) { // yes
			$doneString = '';
			// you need to do a foreach or some look on possibly multiple images. 
			// so either do a for, or foreach
			//for($i=0; $i<$matchcount; $i++) {  // i prefer foreach
			foreach($matches as $match) {
					           // you would need to change $matches[2] to $match[2]
							   // in here you need to go through all of your $matches from above.
							   // I do not know what doneString will look like if you actually have more than one match.
							   // you may have to adjust doneString in this code to be more of a container
							   // and concatenate a result prior to the return doneString below.
				$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);				
				} else { // not too wide, dont add size limit				
					$doneString = str_replace($urlNotags, '<img src="'.$urlNotags.'"/>', $replaced);
					
				}
			}
			return $doneString
		} else { // no
			return $replaced; // send back original string, no image was found
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.