Jump to content

[SOLVED] getimagesize - getting a weird error


GuitarGod

Recommended Posts

Hi,

 

I'm currently using a few functions to create something that will turn text into BBcode. For this I'm using 'preg_replace' as well as an array of code to replace. Example:

 

<?php

function bbcode( $text )
{
    $search = array
    (
        '/\[img\](.*?)\[\/img\]/is',
    );

    $replace = array
    (
        bbcode_image( '$1' ),
    );

    $text = preg_replace( $search, $replace, $text );

    return $text;
    
}

?>

 

The above function should call on another function called 'bbcode_image', using the image URL as the main argument, which it does - to some extent.

 

<?php

function bbcode_image( $url )
{
    // If I simply return the argument '$url', I'll get the image URL, which is good
    // but if I try to use '$url' in the 'getimagesize' function, I get an error :S

    $size = getimagesize( $url );

    return $size[0];
}

?>

 

The error I get  is "Warning: getimagesize($1) [function.getimagesize]: failed to open stream: No such file or directory in W:\www\lkgb\files\functions.php ".

 

Why is it showing '$1' when it should show the image url? Any ideas? I'm sorry for the poor explanation, I'm not good at explaining things, I hope you understand the problem :)

You need to pass preg_replace() a string to be eval'd.  What you're doing is passing in a return value of a function...Try:

<?php

function bbcode( $text )
{
    $search = array
    (
        '/\[img\](.*?)\[\/img\]/ise',
    );

    $replace = array
    (
        "bbcode_image( '$1' )",
    );

    $text = preg_replace( $search, $replace, $text );

    return $text;
    
}

?>

 

Note the 'e' modifier on the regex as well.

Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

 

$1 = invalid variable..

Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

 

$1 = invalid variable..

 

i thought that is what they were doing too...but they are using preg_replace's ability to execute PHP code on the matches...

Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

 

$1 = invalid variable..

 

They're using preg_replace.  It's perfectly valid in that context.

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.