GuitarGod Posted November 4, 2008 Share Posted November 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/ Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 bbcode_image( '$1' ), should be bbcode_image( $1 ), Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682348 Share on other sites More sharing options...
GuitarGod Posted November 4, 2008 Author Share Posted November 4, 2008 I tried that in the beginning but it gave me an error: Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in W:\www\lkgb\files\functions.php This one really does puzzle me :-\ Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682351 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682353 Share on other sites More sharing options...
Andy-H Posted November 4, 2008 Share Posted November 4, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682357 Share on other sites More sharing options...
rhodesa Posted November 4, 2008 Share Posted November 4, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682360 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682362 Share on other sites More sharing options...
Andy-H Posted November 4, 2008 Share Posted November 4, 2008 I'm no good with regular expressions (at all), been meaning to read your tutorial. lol Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682364 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682367 Share on other sites More sharing options...
GuitarGod Posted November 4, 2008 Author Share Posted November 4, 2008 DarkWater's solution he wrote a few posts ago did the trick, thanks man I owe you one Quote Link to comment https://forums.phpfreaks.com/topic/131394-solved-getimagesize-getting-a-weird-error/#findComment-682373 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.