Jump to content

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.

I'm no good with regular expressions (at all), been meaning to read your tutorial. lol

 

The second tutorial goes over all of the matching stuff. =P  I'm in the processing of writing it right now (I'm like halfway done).

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.