簡體   English   中英

Word Doc上的file_get_contents

[英]file_get_contents on Word Doc

我正在使用以下代碼,嘗試使用PHP在Word文檔中查找“術語”。 當然,這不是打開像Word文檔這樣的二進制文件的正確方法,但是“ $ fileContent”中格式錯誤的字符串對我來說已經足夠了。 但是,當搜索當前在文檔內部的術語時,“ stripos”功能無法按預期工作。

$fileContent = file_get_contents($filePath);
$posFileContent = stripos($fileContent,$term);
if ($posFileContent !== false) {
    echo "Found!!";
    $value += $FACTOR_SEC;
}

觀察:在$ fileContent上執行var_dump可顯示文檔的正確內容,當然還有格式錯誤的問題,但該術語仍然存在。

更多信息:

后續代碼var_dump($項)

string(10)“創新”

后續代碼var_dump($ fileContent)

字符串(10240)“ ࡱ ; 根條目 微軟Word-Dokument MSWordDocWord文件8 9 q[Z ZNormal1$ * $ 3B * OJQJCJmH s KHPJnHtH ^ JaJ_H9BA @ BAbsatz-StandardschriftartF FHeading x$ OJQJCJPJ ^ JaJ.B。 文本主體 x/ List ^ J @“” @標題 x x$ CJ6 ^ JaJ]&2&Index $ ^Jd ddPG TimesNewRoman5 Symbol3& ArialG TimesNewRoman5 SimSun5 MangalG MicrosoftYaHei5 MangalB h “ 5_ 5_'00 Oh +' 0| 8 @ LXdp 0@@@ ...... {@ .. @@ M 0 Caolan80 $d bb Lambda發展關於我們Lambda開發創新的軟件產品,帶領我們的客戶走上一條通往成功-我們專注於移動應用程序,Web工具和管理系統-我們的團隊參與了整個過程,從產生想法開始,一直貫穿產品規范,直到以適當的技術實施它。 jl CJ> * 5aJ \\ OJQJ / :; B * ph“”“ CJ @ 6> * 5aJ \\ OJQJCJ $> * 5aJ $ \\ CJ8> 5aJ8 \\( <> $ a $'' / =!n'' n# n$ n3P(20 ՜. +, D ՜. + ,\\根目錄 y F CompObj jOle 1Table iSummaryInformation( WordDocument $DocumentSummaryInformation8 t ''

經過兩天的奮斗,這是答案:

Microsoft Word編碼在所有“真實字符”之間添加了“ \\ 0”字符,因此基本上“ hello”一詞實際上是“ h \\ 0e \\ 0l \\ 0l \\ 0o \\ 0”。

在文檔內部進行搜索的方式是:

$fileContent = file_get_contents($filePath);
$termArray = str_split($term);
$newTerm = '';
foreach ($termArray as $charTerm) {
    $newTerm = $newTerm.$charTerm;
    $newTerm = $newTerm."\0";
}
if (stripos($fileContent,$newTerm) !== false) {
    // Term found in doc
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM