0

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:

<?php
// 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);
}
?>

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">`
derobert
  • 49,731
  • 15
  • 94
  • 124
brinard sweeting
  • 249
  • 1
  • 3
  • 8
  • `but it doesn't parse correctly:` Any specific errors? Are you grabbing unknown external HTML? – Paul Dessert Jan 31 '12 at 21:11
  • 3
    This code looks needlessly convoluted. Can you post your intentions? – Halcyon Jan 31 '12 at 21:13
  • Paul, yes i am grabbing external HTML but it is known for certain rules have been put in place to users that will cause the format to come out as expected by the script. The problem is it doesn't get the correct image, it just follows the next block of code just like the one posted above except everything is for e.g. $png2, $jpg2, etc for the second image. The code above is for the first image/ – brinard sweeting Jan 31 '12 at 21:30
  • 1
    I think you're looking for [How to parse and process HTML with PHP?](http://stackoverflow.com/q/3577641/27727) or maybe [Robust, Mature HTML Parser for PHP](http://stackoverflow.com/q/292926/27727). [This answer](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) explains why. – derobert Jan 31 '12 at 21:39

3 Answers3

0

The errors in your code are just

    $replace1 = $png1;
    $html_part = preg_replace($find, $replace, $html_part);

and

    $replace1 = $jpg1;
    $html_part = preg_replace($find, $replace, $html_part);

- you set $replace1, but you use $replace.

Armali
  • 18,255
  • 14
  • 57
  • 171
0

Your regex is not contained correctly, replace the '#' with '/'. So like this:

preg_match('/cid:([^"@]*).png@([^"]*)/', $html_part)

UPDATE

My mistake, your regex is fine. I will have another look.

Can you possibly supply an example of $html_part?

Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • An example of $html_part: `Bold Text. Italicized Text. Underlined Text. Plain Unformatted Text. Description: Description: Description: cid:image001.png@01CCC701.5A896430Description: Description: cid:image002.jpg@01CCCB1D.D3A29740Description: Description: cid:image003.jpg@01CCCB1D.D3A29740` – brinard sweeting Jan 31 '12 at 21:26
  • @brinardsweeting: I've added your example to your question. Its easiest for people trying to answer if the question details are all in the question (otherwise, they'd have to read all the comments on all the answers to chase down details). You can edit your question by clicking the "edit" link which is under the tags. – derobert Jan 31 '12 at 21:35
  • Yea i forgot to include those important details, i dont usually do that. Thanks – brinard sweeting Jan 31 '12 at 21:46
0

Stylistic tip: instead of multiple regexes whose only difference is the file extension, how about:

if (preg_match('#cid:([^"@]*).(gif|png|jpg|bmp)@([^"]*)#', $html_part)) {
    $find = '#cid:([^"@]*).{$html_part[2]}@([^"]*)#';
                           ^^^^^^^^^^^^^^^---captured file extension

and just capture the file extension found? That'd save you having 4 copies of nearly identical regexes, do it all in a single tesing cycle

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • How would it know which image extension to replace it with? Multiple 'cid:' references inserted into $html_part. I am doing this for a set rule of up to 5 images. So up to for e.g. $png1, $png2, $png3, $png4, $png5. Using your code you posted, after it has replaced with the image. When another image is found how would it know to differentiate to use $png2, $jpg2? etc. and for the next found image....$png3, $jpg3? – brinard sweeting Jan 31 '12 at 21:54