0

変数を置換するための正規表現パターンの記述についてサポートが必要です。

解析用の入力データ
文字列:
/hello/world/1111/2/3/4
/hello/world/2222/2/3/4
/hello/world/3333/2/3/4
/hello/world/3333
/hello /世界/1111

値を置き換える = 一部

アウト
/hello/world/some/2/3/4
/hello/world/some/2/3/4
/hello/world/some/2/3/4
/hello/world/some
/hello/world/some

4

3 に答える 3

0

入力番号が常に 4 桁の場合、

$input = preg_replace("~/\d{4}(/.*)?$~", "some$1", $input);
于 2013-11-14T15:56:13.120 に答える
0
$out = preg_replace("#^/(\w+)/(\w+)/\d{4}(/.*)?$#", "/$1/$2/some$3", $in);

これにより、正しい置換が行われます。ここに簡単な説明があります。

^         start of line
(\w+)     >1 literals
\d{4}     exactly 4 digits
(/.*)?    anything starting with / or nothing at all
$         end of line
于 2013-11-14T15:55:08.297 に答える