Jump to content

I'm being retarded :S


Clarkeez

Recommended Posts

Hey all, I think I'm being a bit dumb but I can't figure the damn thing out..

 

Basically, I've built a VERY simple CMS that runs from a MySQL db..

 

I have recently implemented a image resizing function that needs to be used when anyone that posts an image using

[img=www.exmaple.com/yea.jpg]

 

Here is have my image function which is fired up when displaying the post body from the DB on the website..

It basically just changes the bbcode version of IMG to the html version

<?php
function img($input) {
    $start_arr = array('[img=', ']');
    $finish_arr = array('<img src="', '"/>');
    $bbsafe = str_replace($start_arr, $finish_arr, $input);
}
?>

 

Now the complex part starts because I need every image to run through the image resizing function I have.

Obviously this isn't valid but this would work if it would let me..

<?php
    $start_arr = array('[img=', ']');
    $finish_arr = array('<img src="'.resize('', '')"/>'); // <--
?>

 

This below works fine but it needs to implimented in such a way it will work with the above function too

<?php echo resize('http://www.p.co.uk/pic.png'); ?>

 

Any guidance on this will make me very chuffed :)

Link to comment
https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/
Share on other sites

Ok.. my original post was a tad confusing so im going to rewrite it in a different way..

 

I need to be able to find the following in a string..

<img src="http://url.png"/>

 

and take the url and use it in a function, something like this..

<?php $test = myfunction('http://url.png'); ?>

Link to comment
https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/#findComment-1237764
Share on other sites

use preg_match on your string to extract the url. 

 

A regex like this, while not impervious to some slight format issues, will do what you need.  The better you control the format of the img tag, the more reliable the match. For example, the match will have a problem if your tag has "http://ur.png" />" rather than http://url.png"/>.  That could be improved upon but I didn't see the point in adding more complexity to it without knowing the context in which it will be used.

 


$source = '';

$matchcount = preg_match('!!i', $source, $matches);

if ($matchcount) {
  var_dump($matches);
} else {
  echo "no match found";
}

 

If matched, the url will be in $matches[2]

Link to comment
https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/#findComment-1237772
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.