1

これを回そうとしています:

href="/wp-content/themes/tray/img/celebrity_photos/photo.jpg"

の中へ:

href="/img/celebrity_photos/photo.jpg"

だから私は単にURLから削除しようとしています。 /wp-content/themes/tray/

アンカーパスごとに変数を作成するプラグインのPHPコードは次のとおりです。

$this->imageURL = '/' . $this->path . '/' . $this->filename;

だから私は言いたいです:

$this->imageURL = '/' . $this->path -/wp-content/themes/tray/ . '/' . $this->filename;

PHP substr()strpos()

4

3 に答える 3

4

とすれば:

$this->imageURL = '/' . $this->path . '/' . $this->filename;
$remove = "/wp-content/themes/tray";

既知のプレフィックスが存在する場合、これを削除する方法は次のとおりです。

if (strpos($this->imageURL, $remove) === 0) {
    $this->imageURL = substr($this->imageURL, strlen($remove));
}

それが常に存在することが確実な場合は、if状態を失う可能性もあります。

于 2012-04-10T23:35:20.770 に答える
2

これは1つのオプションです。

$h="/wp-content/themes/tray/img/celebrity_photos/photo-on-4-6-12-at-3-23-pm.jpg";

$prefix="/wp-content/themes/tray/";

print str_replace($prefix, "/", $h, 1);

の左側に固定されないという1つの大きな欠陥があり$hます。これを行うには、正規表現(処理が重い)を使用するか、を実行する前にプレフィックスの位置を検出するものでこれをラップする必要がありますstr_replace()

$h="/wp-content/themes/tray/img/celebrity_photos/photo-on-4-6-12-at-3-23-pm.jpg";

$prefix="/wp-content/themes/tray/";

if (strpos(" ".$h, $prefix) == 1)
  $result = str_replace($prefix, "/", $h, 1);
else
  $result = $h;

print $result;

この重要な要素に注意してください。プレフィックスはスラッシュで終わります。「trayn」や「traypse」のような他のテーマと一致させたくありません。特定のユースケースのためだけに物事を書くことに注意してください。コードがどのように壊れるかを常に把握し、問題のある架空のユースケースをプログラムしてください。

于 2012-04-10T23:36:56.507 に答える
1

これを試して :

$href = str_replace("/wp-content/themes/tray","",$href);

またはあなたの特定のケースでは、このようなもの:

$this->imageURL = '/' . str_replace("/wp-content/themes/tray/","",$this->path) . '/' . $this->filename;
于 2012-04-10T23:33:45.537 に答える