Jump to content

Repeating process in a function


Clarkeez

Recommended Posts

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 :)

Link to comment
Share on other sites

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++){ 

                     

                }

Link to comment
Share on other sites

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 :P ?

Link to comment
Share on other sites

Ok so its still only resizing first image :shrug:

<?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

	}

}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]; //




		}	

}

Link to comment
Share on other sites

Thanks again for this but

Still not working, still only changing the first one.  :confused:

 

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.

Link to comment
Share on other sites

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="

Link to comment
Share on other sites

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-'"..

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.