Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bipin's avatar
Level 2

how to get table and text value of phpword

here i m try to read table as well as text value which i m getting from doc file(getting from database) here is my code

              if($element instanceof PhpOffice\PhpWord\Element\Text) why this line is not working 

i had taken this code from github

      https://github.com/PHPOffice/PHPWord/issues/768

but it not working please suggest any solution what i m try to do is converting doc into html so i can display it into browser

0 likes
12 replies
bashy's avatar

What is the error or what do you see?

bipin's avatar
Level 2

it not going in if condition i have value i cheched it using dd

bipin's avatar
Level 2
   foreach($phpWord->getSections() as $section) {
                        foreach($section->getElements() as $element) {
                            if($element instanceof PhpOffice\PhpWord\Element\Text) 
            {
                                dd('h');
                                echo($element->getText() . "<br><br>");
                                                             
                           }
     else{ 
 it come here instead of if condition
   }
 }
bashy's avatar

And what about dd($element); to see the type?

bipin's avatar
Level 2

@bashy it have array value

           #Text: array:1 [▶]
            #Table: array:1 [▶]
            #TextRun:array:1 [▶]
           

and i want to access Table value

        if($element instanceof PhpOffice\PhpWord\Element\Table) 

since i have Table in element array so why above if condition not working it getting out if condition(else)

bipin's avatar
Level 2

yes, if i use this if condition if(!$element->getText()==null) then it work perfectly

bashy's avatar

If this is the response, you can't do if ($element instanceof Table) since it's not an instance of.

#Text: array:1 [▶]
#Table: array:1 [▶]
#TextRun:array:1 [▶]
Kaustubh's avatar

@bashy is there any package which convert doc, docx or any other file to PDF

bashy's avatar

@Kaustubh I don't know of any myself. Had a search around? Saw a few from "laravel doc to pdf" search.

bipin's avatar
Level 2

@bashy element->getText working ,is there any other way

bashy's avatar

@bipin Sorry, haven't looked at anything like this before.

Please or to participate in this conversation.