-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 代码,它将在页面呈现后替换这些链接中的 flder 名称。

我要替换的只是替换词之间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 (mixed $pattern , mixed $replacement , mixed $subject )

这意味着: $pattern - 你想在你的字符串中替换的模式(我们的例子 $sourceCode),比如"/images\/[a-zA-Z]\//". 您可以在此处阅读有关 regexp 语法的信息。

$replacement - 你想要放置的文本而不是模式。由于我们正在寻找看起来像“images/SOME_TEXT/”的所有内容 - 我们正在替换整个模式。为了正确填充src属性,我们将替换为"image/" . $size . "/"。如果我们写一个 $size 作为替换,我们会得到我们的 $blalba as "<img src=\"http://localhost/test/smallima1.jpg\" width=\"100\" height=\"100\" alt=\"ima1\">"。注意smallima1.jpg(以防万一$size = "small")。

PS 注意每个". 他们阻止 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

Providing that all your images are under the same path, I recommend you leave regular expressions alone (this time), and opt for a much easier explode() method.

Using the PHP explode() function you can split a string into an array using a delimiter.

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

This should give you something like this -

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);

Now you are left with -

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

Providing the URL's are the same for all images, you can simply replace the fourth element ($arr[3]) with the relevant size and then reassemble your URL using the implode() function.

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

Relevant documentation -

于 2012-05-06T14:24:26.617 回答