2012-01-31 22 views
-1

このif文を次のようにして取得しようとしています。最初の文字列の位置が.pngの場合、haystackから$ png1を取得しますが、最初の文字列の位置が.jpgの場合、 haystackから$ jpg1を取得しますが、それが.gifの場合はhaystackから$ gif1を取得し、そうでなければ文字列の位置は.bmpですので$ bmp1を取得します。/else/elseif文の構文解析が正しくない場合

これは私が試したものですが、それは、正しく解析しません:

/***************************** 1st image in email**********************************/ 
     // 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); 
    } 

と5枚の画像の合計のために示さ繰り返し 問題は、私が望んでいただろうとの文が返されない場合です。彼らはただで、表示のために追加改行で、

例の$ html_part(これを5回繰り返すの規模で)全体のシーケンスにマッチした最後のものとの参照を置き換えます

<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:image002[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]">

誰かが私にこの解決策を見つけるのを助けることができますか?ありがとう

+2

重複。 http://stackoverflow.com/questions/9086942/if-statement-parsing-incorrectly#comment11410443_9086942 –

+0

['preg_replace_callback'](http://php.net/preg_replace_callback)と'/cid :(コピー&ペーストの代わりに[^ "@] *)。(gif | bmp | jpeg | png)@([^"] *)/ ''ループ(および場合によっては配列)は、5つの画像すべての処理を統合することができます。 – mario

+0

質問は重複しないでください。これを削除してください。 – hakre

答えて

0

$replaceを使用するときは、$replace1を使用しています。

それはあなたの質問では、単にタイプミスなら、これは同じことを行うために、よりコンパクトな方法である:

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

私は最終的にはPHPの素敵な変数変数

ための非OOPの使用を発見したかもしれないと考えています
// 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 
     } 
    } 

この例では、$ extensionの値は 'jpg1'になります。したがって、$$ extensionがisset()に渡された場合、関数は$ jpg1が設定されているかどうかを確認します。代わりに、あなたはそれを探しているが、一度ファイル拡張子を識別して、スイッチを使用する方が良いだろう結果を見つけるまで繰り返しするpreg_matchを呼び出すの

(ELSEIFで結構です)ように:もともと投稿コードについて

クイックコメント1つのハードコア文字列の比較だけが必要です。

関連する問題