如果语句解析不正确
我想要得到这个如果语句如下: 如果第一个字符串位置是.png,然后从草垛获得$ png1,但如果第一个字符串位置是.jpg,则获取$从草垛JPG1,但如果.gif,而得到草垛$ GIF1,否则,如果他们没有被发现,则字符串位置的.bmp因此获得$ BMP1如果语句解析不正确
这是我尝试过,但它不”吨正确解析:
<?php
// if first occurence is .png get $png1 needle from haystack
if (preg_match('#cid:([^"@]*)[email protected]([^"]*)#', $html_part))
{ $find = '#cid:([^"@]*)[email protected]([^"]*)#';
$replace1 = $png1;
$html_part = preg_replace($find, $replace, $html_part);
}
// if first occurence is .jpg get $jpg1 needle from haystack
elseif (preg_match('#cid:([^"@]*)[email protected]([^"]*)#', $html_part))
{ $find = '#cid:([^"@]*)[email protected]([^"]*)#';
$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:([^"@]*)[email protected]([^"]*)#', $html_part))
{ $find = '#cid:([^"@]*)[email protected]([^"]*)#';
$replace = $gif1;
$html_part = preg_replace($find, $replace, $html_part);
}
// if first occurence is .bmp then get $bmp1 needle from haystack
else
{ $find = '#cid:([^"@]*)[email protected]([^"]*)#';
$replace = $bmp1;
$html_part = preg_replace($find, $replace, $html_part);
}
?>
一个例子$html_part
,添加了用于显示换行符,是:
<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:[email protected]"
alt="Description: Description: Description: cid:[email protected]">
<img width=153 height=145 id="Picture_x0020_2" src="cid:[email protected]"
alt="Description: Description: cid:[email protected]"><img width=182 height=123
id="Picture_x0020_1" src="cid:[email protected]"
alt="Description: Description: cid:[email protected]">`
您的正则表达式没有被正确包含,用'/'替换'#'。所以像这样:
preg_match('/cid:([^"@]*)[email protected]([^"]*)/', $html_part)
UPDATE
我的错误,你的正则表达式是罚款。我会再看一次。
你可能提供一个$html_part
的例子吗?
$ html_part的示例: '粗体文本。斜体文本。加下划线文字。平原无格式文本。 ' – 2012-01-31 21:26:20
@brinardsweeting:我已将您的示例添加到您的问题中。对于试图回答问题细节是否全部存在问题的人来说,它是最容易的(否则,他们必须阅读关于所有答案的所有评论以追查细节)。您可以通过点击标签下的“编辑”链接来编辑您的问题。 – derobert 2012-01-31 21:35:26
是的,我忘了包括那些重要的细节,我通常不这样做。谢谢 – 2012-01-31 21:46:15
文体提示:而不是多个正则表达式的唯一区别是文件扩展名,如何:
if (preg_match('#cid:([^"@]*).(gif|png|jpg|bmp)@([^"]*)#', $html_part)) {
$find = '#cid:([^"@]*).{$html_part[2]}@([^"]*)#';
^^^^^^^^^^^^^^^---captured file extension
和刚刚捕获的文件扩展名发现了什么?这会节省你具有几乎相同的正则表达式4份,做这一切在一个单一的tesing周期
它如何知道用哪个图像扩展名替换它?插入到$ html_part的多个'cid:'引用。我这样做是为了达到5张图像的规则。所以最多可以用于例如$ png1,$ png2,$ png3,$ png4,$ png5。 使用您的代码发布后,代替图像。当发现另一个图像时,它会如何区分使用$ png2,$ jpg2?等等,并为下一个发现的图像.... $ png3,$ jpg3? – 2012-01-31 21:54:46
在你的代码中的错误只是
$replace1 = $png1;
$html_part = preg_replace($find, $replace, $html_part);
和
$replace1 = $jpg1;
$html_part = preg_replace($find, $replace, $html_part);
- 设置$replace1
,但您使用$replace
。
'但它不能正确解析:'任何具体的错误?你在抓取未知的外部HTML吗? – 2012-01-31 21:11:16
此代码看起来不必要的复杂。你能发表你的意图吗? – Halcyon 2012-01-31 21:13:54
保罗,是的,我抓住外部的HTML,但它是已知的某些规则已放到用户将导致格式出现如预期的脚本。问题是它没有得到正确的图像,它只是紧跟着下面的代码块,就像上面发布的代码块,除了一切都是为了例如$ png2,$ jpg2等用于第二张图片。上面的代码是第一张图片/ – 2012-01-31 21:30:12