0

タイトルを付ける方法はわかりませんが、次のような正規表現を使用してください。

preg_replace('#static\/(.*)(\/|\.[a-zA-Z0-9]{2,4})#', 'path=$1$2');

と一致する必要がstatic/path/to/image.jpgありstatic/path/to/dir/ます。ここで、2番目のパターン(ディレクトリ)と一致する場合は先頭のスラッシュに置き換えますが、ファイル名(1番目のパターン)と一致する場合は先頭のスラッシュなしで置き換えます。

例:

`static/path/to/image.jpg` should be 'path=path/to/image.jpg'
`static/path/to/image.jpg/` should be 'path=path/to/image.jpg'
`static/path/to/dir/` should be 'path=path/to/dir/'

$2簡単に言えば、最後に要求されたファイルと等しい場合は無視したいと思います/。追加する?:ことでうまくいくと思いましたが、私は間違っていました。

そのようなことをする方法はありますか?

4

2 に答える 2

1

パスが URL の末尾にあると仮定します。

preg_replace('#static((?:/[^./]*(?=/))*)(/(?:\w+\.\w+)?)/?$#', 'path=$1$2');

または先読みなし(高速):

preg_replace('#static(/(?:[^./]*/)*)(\w+\.\w+)?/?$#', 'path=$1$2');

編集: OPによる明確化を追加して正規表現を変更しました

于 2013-01-28T08:12:26.977 に答える
0

本質的には、 (パス名が続く場合)に置き換えstatic/ているだけですよね?path=

次に、それを行います:

$result = preg_replace(
    '%static/      # Match static/
    (?=            # only if the following text could be matched here:
     \S+           # one or more non-whitespace characters,
     (?:           # followed by
      /            # a slash
     |             # or
      \.\w{2,4}    # a filename extension
     )             # End of alternation.
     (?!\S)        # Make sure that there is no non-space character here
    )              # End of lookahead.%x', 
    'path=', $subject);
于 2013-01-28T08:03:00.263 に答える