Clarkeez Posted July 2, 2011 Share Posted July 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/ Share on other sites More sharing options...
Clarkeez Posted July 2, 2011 Author Share Posted July 2, 2011 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/#findComment-1237764 Share on other sites More sharing options...
gizmola Posted July 2, 2011 Share Posted July 2, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/#findComment-1237772 Share on other sites More sharing options...
Clarkeez Posted July 3, 2011 Author Share Posted July 3, 2011 Gizmola, Thanks for you reply mate, I managed to solve the problem with the regex preg_match you provided. Much appriciated! Quote Link to comment https://forums.phpfreaks.com/topic/240970-im-being-retarded-s/#findComment-1237775 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.