Question

I am using the following code attempting to find a "term" in a word document using PHP. Of course this is not a right way of opening a binary file like a word document, but the malformatted string in "$fileContent" is good enough for me. However, the "stripos" function is not working as expected when searching for a term that currently is inside the doc.

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

Observation: doing a var_dump on $fileContent shows the proper content of the doc, with its malformat issues of course, but still the term is there.

More info:

var_dump($term)

string(10) "innovative"

var_dump($fileContent)

string(10240) "��ࡱ�;�� ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Root Entry������������������������������������������������������������������������ ��������!���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� �FMicrosoft Word-Dokument MSWordDocWord.Document.8�9�q [Z��ZNormal1$*$3B*OJQJCJmH sH KHPJnHtH^JaJ_H9BA@���BAbsatz-StandardschriftartF�FHeading ���x$OJQJCJPJ^JaJ.B. Text body ��x / List^J@""@Caption �x�x$CJ6^JaJ]&�2&Index$^Jd����� � ddPG�Times New Roman5�Symbol3&�ArialG��Times New Roman5�SimSun5�MangalG�Microsoft YaHei5��MangalB��h��"�5_��5_' 0 0��������Oh��+'��0|8 @ LXd p��0@@@�{� �.�@��M ��0� Caolan80 $d��������������b ��Lambda Developments About us Lambda develops innovative software products leading our clients in a road to success.We specialize in mobile apps, web tools and management systems.Our team is involved in the entire process, beginning where an idea is born, going through the product specification, until its implementation in an appropriate technology. &*:>��j l � � ����������CJ>*5aJ\OJQJ/:;B*ph"""CJ@�6>*5aJ\OJQJCJ$>*5aJ$\CJ8>5aJ8\ ( <>� �����$a$"��/ ��=!�n"�n#�n$�n3P(20����՜.��+,��D��՜.��+,��\����Root Entry�������� �F�CompObj����jOle ��������1Table������������iSummaryInformation(�����WordDocument������������$DocumentSummaryInformation8������������ t����������������"

No correct solution

OTHER TIPS

After two days of struggling this is the answer:

Microsoft Word encoding adds "\0" characters between all "real characters", so basically the word "hello" is actually "h\0e\0l\0l\0o\0".

The way to search inside the doc is:

$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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top