简体   繁体   中英

Edit xml as simple text file in PHP

I want to replace server's address inside xml file. Placed placeholder %scr_path% on line.

<property id="urlGenerateImage">%scr_path%/imgcap.php</property> 

Using following code

$path=$wsurl."core/contents/tests";
//read the entire string
$str=implode("\n",file('../includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml'));

$fp=fopen('../includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml','w');
//replace something in the file string 
$str=str_replace('%scr_path%',$path,$str);

//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));

Getting bunch of errors about wrong file path. Checked twice path. What's wrong?

It all depends what directory your script is running in. Since you're using a relative path name (../), you'd better be sure you know what directory the script currently is in. Or, even better, set it at the beginning of your script so it will always work.

$path = $wsurl."core/contents/tests";

// change directory to project root
chdir("/your/project/directory");

// read the file into a string
$filename = 'includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml';
$str = file_get_contents($filename);

// replace token
$str = str_replace('%scr_path%',$path,$str);

// save file
file_put_contents($filename, $str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM