at the moment i am passing in the background image using $vars->bgpath if i echod out this variable it would be images/accounts/bg1.jpg now i want to change the variable now so that is uses a different background image with the same naming convention, for example i want to change it to images/accounts/pdf_1.jpg would i do this with regex? as it needs to be done dynamically. $pdf->Image($_SERVER['DOCUMENT_ROOT'].'/'.$vars->bgpath, 0, 0, 91);
just to clarify: $change_var_image = $vars->bgpath; //regex needs to be done $pdf->Image($_SERVER['DOCUMENT_ROOT'].'/'.$change_var_image, 0, 0, 91);
You won't need a regex for that. Of course you could use one but a simple str_replace does everything you want: $newFilename = "pdf_1.jpg"; $change_var_image = str_replace($vars->bgpath, $newFilename, basename($vars->bgpath)); PHP:
thanks for the code the problem is $vars->bgpath; passes in different values dynamically for example it may pass in: images/accounts/bg1.jpg images/accounts/bg2.jpg images/accounts/bg3.jpg i now need to change the $change_var_image to pdf_1.jpg or pdf_2.jpg depending on the number it passes in. for example if it passes in images/accounts/bg3.jpg then pdf_3.jpg needs to be set.
ahh allright... i got that wrong Then a regex is the right thing. Do the filenames always are like bg#.jpg? Then a simple regex like this will work: $change_var_image = preg_replace("/^.*/bg(\d+).jpg/", "images/accounts/pdf_$1.jpg", $vars->bgpath); PHP: