Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用str_replace或preg_replace更改图像路径_Php_Str Replace - Fatal编程技术网

Php 使用str_replace或preg_replace更改图像路径

Php 使用str_replace或preg_replace更改图像路径,php,str-replace,Php,Str Replace,朋友们好,使用str_replace(),我很难得到想要的结果PHP的函数 我的代码是这样的: <?php ob_start (); /* Start Buffering */ $layout = 'climate'; ?> <div> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque.</p> <img src="images

朋友们好,使用
str_replace(),我很难得到想要的结果PHP的函数

我的代码是这样的:

<?php ob_start (); /* Start Buffering */

$layout = 'climate';

?>

<div>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque.</p>

<img src="images/industry/pic1.jpg"/>
<img src="products/images/industry/pic2.jpg"/>
<img src="samples/products/images/industry/pic3.jpg"/>

</div>

<?php ob_get_contents();  /* Get the Buffer */

$content = str_replace('/desktop/', $layout . '/images/', $layout);

echo $content;

ob_end_flush (); /* Send Output to the browser */

?>
最后:

<?php ob_start (); /* Start Buffering */

$layout = 'climate';

?>

<div>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque.</p>

<img src="images/industry/pic1.jpg"/>
<img src="products/images/industry/pic2.jpg"/>
<img src="samples/products/images/industry/pic3.jpg"/>

</div>

<?php ob_get_contents();  /* Get the Buffer */

$content = str_replace('/desktop/', $layout . '/images/', $layout);

echo $content;

ob_end_flush (); /* Send Output to the browser */

?>

如果您觉得任何其他解决方案都比这个好,请随时提出建议。

您可以获取缓冲区并用关闭它,然后进行替换并回显
$content
,它将发送到浏览器

<div>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque.</p>

<img src="images/climate/pic1.jpg"/>
<img src="products/images/climate/pic2.jpg"/>
<img src="samples/products/images/climate/pic3.jpg"/>

</div>
$content = ob_get_clean();  
$content = str_replace('/industry/', '/' . $layout . '/', $content);
echo $content;

ob_get_contents()未分配给任何内容。
$content=ob_get_contents()