Mr Candu Posted July 27, 2009 Share Posted July 27, 2009 Hi. My code uses preg_replace to extract an image id (16) from a text string (""). The id is then sent to another function and the image information is returned. Unfortunatley the id seems to be passed as some kind of text string that cannot be converted to an interger. As a test example for you to look at, the following code returns 16,0 when it should be returning 16,16 If you have any advice on why this is happening I would be very greatful ...... i'm slowly losing the will to live! Regards, Mat. <?php function create_image($imgid) { return $imgid.",".(int)$imgid; } $text = "[img=16]"; $text = preg_replace("/\[img\=(.+?)\]/",create_image('$1'),$text); echo $text."<br>"; ?> Quote Link to comment Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 What you do now is that you simply send the string $1 to your function. You should use preg_replace_callback instead: $text = preg_replace_callback('/\[img\=(.+?)\]/', create_function('$matches', 'return create_image($matches[1]);'), $text); Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2009 Share Posted July 27, 2009 What you are doing doesn't make sense. You are trying to replace text based upon the result of the function which requires for the replacement to be made first. You need to break out the logic. <?php function create_image($imgid) { return $imgid.",".(int)$imgid; } $text = "[img=16]http://"; $id = preg_replace("/[img=(.+?)]/", '$1', $text); echo create_image($id); //Output: 16,16 ?> Quote Link to comment Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 I agree although preg_match_callback will do exactly that. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 27, 2009 Share Posted July 27, 2009 I believe it would. But, you're throwing an extra step in there. $text = preg_replace_callback('/\[img\=(.+?)\]/', create_function('$matches', 'return create_image($matches[1]);'), $text); Could be $text = preg_replace('/\[img\=(.+?)\]/e', 'create_image("\1")', $text); And really it could be: $text = preg_replace('/\[img\=([0-9]+)\]/e', 'create_image("\1")', $text); Not sure if preg_replace_callback is actually quicker or not, but I would imagine no because of the create_function. Quote Link to comment Share on other sites More sharing options...
Mr Candu Posted July 27, 2009 Author Share Posted July 27, 2009 Thanks for you help guys. Vineld's original suggestion works well for me, so thanks very much. I probably should have explained in more detail what i am ultimately trying to achieve: The code is part of a CMS system. The function will parse a text field from a db on output and replace all tags (of which their could be many) with the appropriate HTML <img src=whateverSrc /> tag. The create_image function will query the db to retrieve the image src for the given id. $text = preg_replace_callback('/\[img\=(.+?)\]/', create_function('$matches', 'return create_image($matches[1]);'), $text); I not sure if this is the most efficient way of doing things, but it is the only option the works out of suggestions posted. Cobins suggestion works only for 1 occurrence of an tag, though I'm rubbish at reg ex and maybe it could be adjusted there? $text = preg_replace('/\[img\=([0-9]+)\]/e', 'create_image("\1")', $text); Thanks for you help. Quote Link to comment 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.