-2

ストリング :

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

PREG_REPLACE_CALLBACK コード (PHP) :

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

出力 ($マッチ) :

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 配列) は、上記の印刷と同じように見えるはずです。

読んでいただきありがとうございます。

4

1 に答える 1

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 の前にパイプに削除しますか?文字列でこれを実行し、結果として正規表現を更新しますか? ''ucfirst'' 文字列 (最初のパイプの前の最後の文字列?) を識別するルール

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

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

于 2011-10-01T23:28:02.847 に答える