如果语句解析不正确 [英] If statement parsing incorrectly

查看:118
本文介绍了如果语句解析不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把这个if语句作为:
如果第一个字符串位置是.png,然后从大海捞针获得$ png1,但如果第一个字符串位置是.jpg,那么得到$ jpg1从干草堆,但如果它是.gif,从haystack获取$ gif1,否则如果没有找到它们那么字符串位置是.bmp所以得到$ bmp1

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

示例 $ 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">`


推荐答案

错误在你的代码中只是

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

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

- 你设置 $ replace1 ,但是你使用 $ replace

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

这篇关于如果语句解析不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆