Jump to content

[SOLVED] Get image URL out of string?


Chevy

Recommended Posts

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 IMAGEURL 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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;

}

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.