Jump to content

[SOLVED] preg_replace - integers?


Mr Candu

Recommended Posts

Hi.

 

My code uses preg_replace to extract an image id (16) from a text string ("16"). 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>";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 whateverID 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.

 

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.