1 (edited by druvans 2005-04-14 17:02)

Topic: Help with preg_replace

I am trying to add a new bbcode called [x]druvan@gmail.com[/x], what I want is, whatever value placed inside this code, should be turned into images, for example, email ids.

I created a function called imagize, which basically converts the given text into a png image file, then returns the file name. This part works OK.

Now, I added all this to parser.php file and   

$text = preg_replace("#\[x\](.*?)\[/x\]#", "imagize('$1')", $text);

==>  imagize('druvan@gmail.com')

I am not able to execute  'imagize' function, can someone help me.

Result  <img src=imagize('$1') />

Re: Help with preg_replace

well its because you have put imagize in " "

3 (edited by druvans 2005-04-14 22:47)

Re: Help with preg_replace

able to get it work, trying to integrate to bbcode

<h5><?php $text= "[x]druvans@gmail.com[/x]"; $text=preg_replace('#\[x\](.*?)\[/x\]#e','imagize(\'$1\')',$text); echo $text;  ?></h5>

<?php

/***********************************************************************
Druvan (druvan@gmail.com)
Use it anyway you want it.
************************************************************************/
function imagize($email){

$fontsize=4;
// Create the image
$im = @imagecreatetruecolor( strlen($email)*8, 16 ) or die( "Cannot Initialize new GD image stream" );
//Create background color
$background = imagecolorallocate($im, 255,  255,255);
imagefilledrectangle( $im, 0,0,strlen($email)*8, 16, $background);

// Create foreground color
$foreground = imagecolorallocate($im, 125,  125,125);
// write the string at the top left
imagestring($im,$fontsize, 0, 0, $email, $foreground);


// Using imagepng() results in clearer text compared with imagejpeg()
$filename =substr($email, 0,strrpos($email, '@')).".png";
imagepng($im,$filename);
return "<img src='".$filename."' />";
}

?>


?>

Re: Help with preg_replace

you can't embed an image in the html you need to put the function in another file and call it

5 (edited by druvans 2005-04-14 22:47)

Re: Help with preg_replace

I am not sure what exactly you mean by that.

thanks

6 (edited by spider8 2005-04-27 00:23)

Re: Help with preg_replace

druvans,

you might try to change your code in something like:

$text = preg_replace("#\[x\](.*?)\[/x\]#", imagize($1), $text);

but I'm not sure, whether it works out for you. I have never used a function inside a preg_replace(), so I really don't know about it. If this doesn't work you might choose an iterative approach replacing one email address after the other. A third variant would be to apply your differently modified $text to the eval() function.

There is at least one more source for trouble in your imagize() function, that I found at a first glance. You use only the first part of the email address to determine the filename, so if you have two people with info@domain1.com and info@domain2.com they both will show up with the info.png image which serves for domain2.com.

Re: Help with preg_replace

Thanks. I havent thgt abt  the filenames yet.

I am able to execute and see the values with this line

<h5><?php $text= "[x]druvans@gmail.com[/x]"; $text=preg_replace('#\[x\](.*?)\[/x\]#e','imagize(\'$1\')',$text); echo $text;  ?></h5

I still cldnt figure out, how to integrate it to bbcode

8 (edited by spider8 2005-04-14 23:29)

Re: Help with preg_replace

druvans,

I still don't know exactly, what your problem is.

You are right, despite the ' ' around your function, the content gets eval()ed.

Now let's try this

$text=preg_replace('#\[x\](.*?)\[/x\]#e', "imagize('$1')", $text);

so that the $1 will be translated instead of taken literally.

9 (edited by spider8 2005-04-14 23:37)

Re: Help with preg_replace

Forget my last post, this was nonsense.

I think, I found the missing point. In your last example, you had an 'e' at the end of your regular expression (after the final #) which was missing in your first examply. I guess, this is doing the eval() job.

10 (edited by druvans 2005-04-15 04:21)

Re: Help with preg_replace

thanks guys. I got it working.

http://jobs.gotoguide.org/viewtopic.php?pid=3#p3