-1

私はPHPと正規表現が初めてなので、それを使用していくつかの画像パスを編集するのに助けが必要です.

私の CMS は、次のようなイメージ パスを生成します。

<img src="http://localhost/test/images/normal/ima1.jpg" width="100" height="100" alt="ima1">

私はPHPを使用しており、変数$sizeを持っています。その場合$size = 'small'、パスは

<img src="http://localhost/test/images/small/ima1.jpg" width="100" height="100" alt="ima1">

もしそうなら$size = 'medium'、パスは

<img src="http://localhost/test/images/medium/ima1.jpg" width="100" height="100" alt="ima1">

これらのリンクは CMS によって動的に生成されるため、ページがレンダリングされた後にこれらのリンクの名前を置き換える PHP コードを探しています。

置き換えたいのは、置換する単語の間images//後の単語だけです。

4

3 に答える 3

1

試す$blabla = preg_replace( "/images\/[a-zA-Z]\//" , "images\/" . $size . "\/" , $sourceCode );

今、$blablaランダムな名前です。好きなように変更できます。 $sourceCodeも名前です。置き換えたい文字列に置き換える必要があります。例$sourceCode = "<img src=\"http://localhost/test/images/small/ima1.jpg\" width=\"100\" height=\"100\" alt=\"ima1\">"

関数 preg_replace の構文は次のとおりです: preg_replace (混合 $pattern 、混合 $replacement 、混合 $subject )

$pattern - 文字列で置き換えたいパターン (この例では $sourceCode) など"/images\/[a-zA-Z]\//"ここで正規表現の構文について読むことができます。

$replacement - パターンの代わりに配置するテキスト。「images/SOME_TEXT/」のように見えるすべてのものを探しているので、パターン全体を置き換えています。src属性を正しく埋めるために、置換を として行います"image/" . $size . "/"。置換として単一の $size を記述した場合、 $bralba as が得られます"<img src=\"http://localhost/test/smallima1.jpg\" width=\"100\" height=\"100\" alt=\"ima1\">"smallima1.jpg( の場合) に注意してください$size = "small"

PS every の前のバックスラッシュに注意してください"。彼らは、phpパーサーが文字列入力の終わりであると考えるのを防いでいます。たとえば$name = "The "Batman"";、エラーを返しますが、変数$name = "The \"Batman\"";に代入します。引用符を含む文字列を変数に代入する場合、これらは必須です。The "Batman"$name

于 2012-05-06T14:26:57.513 に答える
0

あなたへの私の質問は、なぜあなたはしたいのですreplaceか?私はあなたのサイトが適切に構成されていればあなたは置き換える必要はありません

私はこのようなものを期待しています

$sizes = array("normal"=>array(500,500),
        "small"=>array(100,100),
        "medium"=>array(300,200));


$selected = "small"  ;
$imageHost= "http://localhost/test/images" ;
$imagePath = "/public_html/test/images" ;
$imageName = "ima1.jpg" ;
$tag = "<img src=\"{$imageHost}/%s/$imageName\" width=\"%d\" height=\"%s\" alt=\"ima1\">";

すべてのサイズを出力する簡単なデモ

echo "<pre>" ;
foreach($sizes as $size => $dim){

    if(!file_exists($imagePath . DIRECTORY_SEPARATOR . $selected . DIRECTORY_SEPARATOR .  $imageName))
    {
        // Am sure you want to either create the image or copu the thumn here 
    }
    echo printf($tag,$size,$dim[0],$dim[1]) . PHP_EOL;
}

出力

<img src="http://localhost/test/images/normal/ima1.jpg" width="500" height="500" alt="ima1">
<img src="http://localhost/test/images/small/ima1.jpg" width="100" height="100" alt="ima1">
<img src="http://localhost/test/images/medium/ima1.jpg" width="300" height="200" alt="ima1">
于 2012-05-06T14:40:02.920 に答える
0

すべての画像が同じパスの下にあるという条件で、正規表現はそのままにして (今回は)、はるかに簡単なexplode()方法を選択することをお勧めします。

PHPexplode()関数を使用すると、区切り文字を使用して文字列を配列に分割できます。

$str = 'http://localhost/test/images/small/ima1.jpg'; 
$arr = explode('/',$str);

これにより、次のようなものが得られるはずです-

Array 
(
    [0] => http:
    [1] =>
    [2] => localhost
    [3] => test
    [4] => images
    [5] => small
    [6] => ima1.jpg
) 

// remove the protocol specification and the empty element.
// You'll see that the `explode()` function actually removes the slashes 
// (including the two at the beginning of the URL in the protocol specification),
// you'll have to return them once you have finished.
array_shift($arr);
array_shift($arr);

今、あなたは残っています -

Array 
(
    [0] => localhost
    [1] => test
    [2] => images
    [3] => small
    [4] => ima1.jpg
) 

URL がすべての画像で同じであれば、4 番目の要素 ( $arr[3]) を適切なサイズに置き換えるだけで、関数を使用して URL を再構成できますimplode()

array_unshift($arr,'/'); // adds an element to the beginning of an array
array_unshift($arr,'http:');
$finalURL = implode('/',$arr);

関連文書 -

于 2012-05-06T14:24:26.617 に答える