2011-09-13 20 views
-2

STRING:正規表現の修正

$string = '{$string#anything#something this string will output default |ucfirst|strtoupper}'; 

PREG_REPLACE_CALLBACK CODE(PHP):

$string = preg_replace_callback('/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/', $matches, $string); 

OUTPUT($マッチ):

Array 
(
    [0] => {$string#anything#something can you hear me? |ucfirst|ucfirst|ucfirst|strtoupper} 
    [1] => string 
    [2] => anything#something 
    [3] => can you hear me? 
    [4] => ucfirst|strtoupper 
) 

要件:{$string this string will output default |ucfirst|strtoupper}の代わりに、私は{$string this string will output default ucfirst|strtoupper}を使用したい(通知:ucfirstの前のパイプサインが削除されます) ;

重要:出力(つまり、$ matches配列)は、上記と同じに見えます。

お読みいただきありがとうございます。私はあなたの関数を変更して、あなたのコードを試してみました

答えて

1

...

<pre> 
<?php 
     $string = '{$string#anything#something this string will output default |ucfirst|strtoupper}'; http://www.magentocommerce.com/support/magento_core_api 
     preg_match('/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/', $string, $matches); 
     var_dump($matches); 
    ?> 
    </pre> 

結果は、それはあなたの期待に合致され

array(5) { 
    [0]=>string(80) "{$string#anything#something this string will output default |ucfirst|strtoupper}" 
    [1]=>string(6) "string" 
    [2]=>string(18) "anything#something" 
    [3]=>string(32) "this string will output default " 
    http://php.net/manual/en/function.preg-replace-callback.php [4]=>string(18) "ucfirst|strtoupper" 
} 

を得たコード?私たちは "この文字列が出力されます"をデフォルトで "あなたは私のことを聞くことができますか?"と置き換えるべきでしょうか? ucfirstを実行する前にパイプに削除したいのですが、 (最初のパイプの前の最後の文字列)を識別するルール

preg_replace_callbackで何をしようとしていますか?この関数は見つかったモチーフごとに関数($ matchesパラメータ)を呼び出します。 ..それはあなたのアイデアですか?

リンクされたドキュメント:http://php.net/manual/en/function.preg-replace-callback.php