php question

Discussion in 'PHP' started by dean5000v, Mar 30, 2010.

  1. #1
    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);
     
    dean5000v, Mar 30, 2010 IP
  2. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just to clarify:

    $change_var_image = $vars->bgpath;
    //regex needs to be done
    $pdf->Image($_SERVER['DOCUMENT_ROOT'].'/'.$change_var_image, 0, 0, 91);
     
    dean5000v, Mar 30, 2010 IP
  3. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    Nyu, Mar 30, 2010 IP
  4. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    dean5000v, Mar 30, 2010 IP
  5. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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:
     
    Nyu, Mar 30, 2010 IP
  6. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thank you very much i have it all sorted out now :)
     
    dean5000v, Mar 30, 2010 IP