如何让 PHP 包含 HTML 文件并更改 DIV 对齐属性?

How do I get PHP to include an HTML file and change DIV align attribute?

如何在我的 php 页面中包含一个 html 文件并将其 divs 标记 align="center" 更改为 align="left"从 HTML 文件加载。

要在 roomchanges.htm 中更改的行:

<div id="Room Changes_1871" align="center" x:publishsource="Excel">

<div id="Room Changes_1871" align="left" x:publishsource="Excel">

我尝试了以下但它不起作用,我是高级新手 php 所以任何帮助世界都将不胜感激:

<?php
$string = include('import/roomchanges.htm');
$string = preg_replace('center', 'left', $string); 
?>

提前感谢您的帮助。

你应该试试:

$string = file_get_contents('import/roomchanges.htm');
$string = preg_replace('center', 'left', $string); 

使用file_get_contents()

<?php
   $string = file_get_contents('import/roomchanges.htm');
   $string = str_replace('center', 'left', $string); 
   print $string;
?>

并且,如果您想再次保存页面:

<?php
   $string = file_get_contents('import/roomchanges.htm');
   $string = str_replace('center', 'left', $string); 
   file_put_contents('import/roomchanges.htm',$string);
?>

您可以将 roomchanges.htm 更改为 roomchanges.php 并在对齐表达式中输入一个变量。

if($a=="b")
   $align="center";
else{
   $align="left";
}

<div id="Room Changes_1871" align="<?=$align?>" x:publishsource="Excel">