-1

I am trying to get this if statement to follow as: if the first string position is .png, then get $png1 from a haystack, but if the first string position is .jpg, then get $jpg1 from the haystack, but if it is .gif, get $gif1 from haystack, else if none of them are found then the string position is .bmp so get $bmp1

Here is what i tried, but it doesn't parse correctly:

/***************************** 1st image in email**********************************/
        // if first occurence is .png get $png1 needle from haystack
    if (preg_match('/cid:([^"@]*).png@([^"]*)/', $html_part))           
    {           $find = '/cid:([^"@]*).png@([^"]*)/';   
                $replace1 = $png1;
                $html_part = preg_replace($find, $replace, $html_part);
    }
        // if first occurence is .jpg get $jpg1 needle from haystack
        elseif (preg_match('/cid:([^"@]*).jpg@([^"]*)/', $html_part)) 
    {           $find = '/cid:([^"@]*).jpg@([^"]*)/';   
                $replace1 = $jpg1;
                $html_part = preg_replace($find, $replace, $html_part);
    }
        // if first occurence is .gif then get $gif1 needle from haystack
        elseif (preg_match('/cid:([^"@]*).gif@([^"]*)/', $html_part)) 
    {           $find = '/cid:([^"@]*).gif@([^"]*)/';
                $replace = $gif1;
                $html_part = preg_replace($find, $replace, $html_part);
    }
        // if first occurence is .bmp then get $bmp1 needle from haystack
        else
    {           $find = '/cid:([^"@]*).bmp@([^"]*)/';
                $replace = $bmp1;
                $html_part = preg_replace($find, $replace, $html_part);
    }

and repeated forth for a total for 5 images The problem is the if statements don't return as i would've hoped. They just replace the reference with the last thing matched in the entire sequence (On a scale of repeating this 5 times)

An example $html_part, with line breaks added for display, is:

<b>Bold Text.</b> <i>Italicized Text.</i> <u>Underlined Text.</u> Plain Unformatted Text. <img width=183 height=183 id="Picture_x0020_3" src="cid:image001.png@01CCCB31.E6A152F0" alt="Description: Description: Description: cid:image001.png@01CCC701.5A896430"> <img width=153 height=145 id="Picture_x0020_2" src="cid:image002.jpg@01CCCB31.E6A152F0" alt="Description: Description: cid:image002.jpg@01CCCB1D.D3A29740"><img width=182 height=123 id="Picture_x0020_1" src="cid:image003.jpg@01CCCB31.E6A152F0" alt="Description: Description: cid:image003.jpg@01CCCB1D.D3A29740">

Could someone help me find a solution for this? Thanks

brinard sweeting
  • 249
  • 1
  • 3
  • 8
  • 2
    duplicate. http://stackoverflow.com/questions/9086942/if-statement-parsing-incorrectly#comment11410443_9086942 – Paul Dessert Feb 01 '12 at 00:01
  • Consider [`preg_replace_callback`](http://php.net/preg_replace_callback) and a single regex like `'/cid:([^"@]*).(gif|bmp|jpeg|png)@([^"]*)/'` instead of copy&pasting. A loop (and possibly an array) can consolidate the handling of all five images. – mario Feb 01 '12 at 00:05
  • Please do not duplicate questions. Please delete this one. – hakre Feb 01 '12 at 16:08

2 Answers2

0

You're using $replace1 when you should be using $replace.

If that's just a typo in your question, this is a more compact way to do the same thing:

if (preg_match('/cid:([^"@]*).(png|jpg|gif|bmp)@([^"]*)/', $html_part, $m)){

    $find = '/cid:([^"@]*).'.$m[2].'@([^"]*)/';

    $replace = $png1;
    if ($m[2] == 'jpg') $replace = $jpg1;
    if ($m[2] == 'gif') $replace = $gif1;
    if ($m[2] == 'bmp') $replace = $bmp1;

    $html_part = preg_replace($find, $replace, $html_part);
}
Cal
  • 7,067
  • 25
  • 28
-1

I believe I may have finally found an non-OOP use for php's lovely variable variable

    // Slightly verbose for demo purposes
    $path = 'C://test.jpg';

    // Get the path info with PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION and PATHINFO_FILENAME.
    $pathinfo = pathinfo($path);

    // Check file extensions first to avoid E_NOTICE of index 'extension'
    if (isset($pathinfo['extension']) !== false)
    {
        // Get the file extension and append '1' as per OP's spec
        $extension = $pathinfo['extension'] . '1';

        // Make sure that the image exists before using it ($jpg1)
        if (isset($$extension) !== false)
        {
            // Funny stuff
        }
    }

In this example the value of $extension ends up being 'jpg1' so if when $$extension is passed to isset() the function checks to see if $jpg1 is set.

Quick comment about originally posted code:

Instead of calling preg_match iteratively until you find the result your looking for it would be better to identify the file extension once and then use a switch (elseif is fine) so as there need only be one hardcore string comparison.

Tecnocat
  • 1,162
  • 1
  • 11
  • 14
CBusBus
  • 2,321
  • 1
  • 18
  • 26