我正在尝试阅读docx文件并替换| * Pre Set * |用户输入的单词。无法替换单词而无法在字符串中找到这样的单词。
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
echo "Cannot open $filename :( "; die;
}
$xml = $zip->getFromName('word/document.xml');
if(strpos($xml,'|*First Name*|')) {
echo 'True';
} else {
echo 'False';
}
//True;
if(strpos($xml,'|*Last Name*|')) {
echo 'True';
} else {
echo 'False';
}
//False
echo $xml;
//Hi! My name is |*First Name*| |*Last Name*|. My Email address is |*Email*|. My contact number is |*Contact Number*|.
$xml = str_replace('|*First Name*|', 'John', $xml);
$xml = str_replace('|*Last Name*|', 'Doe', $xml);
echo $xml;
//Hi! My name is John |*Last Name*|. My Email address is |*Email*|. My contact number is |*Contact Number*|.
这是复制问题的简短脚本。在这种情况下,它工作得很好。它取代了字符串。它在字符串中找到了这个单词,但我不知道为什么在我的主脚本中没有发生同样的事情,我将docx文件读作xml文件。
$old_string = "Hi! My name is |*First Name*| |*Last Name*|. My Email address is |*Email*|. My contact number is |*Contact Number*|.";
if(strpos($old_string,'|*First Name*|')) {
echo 'True';
} else {
echo 'False';
}
//True
if(strpos($old_string,'|*Last Name*|')) {
echo 'True';
} else {
echo 'False';
}
//True
$old_string = str_replace('|*First Name*|', 'John', $old_string);
$old_string = str_replace('|*Last Name*|', 'Doe', $old_string);
echo $old_string;
//Hi! My name is John Doe. My Email address is |*Email*|. My contact number is |*Contact Number*|