Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension for PHPWord/src/PhpWord/IOFactory.php::extractVariables #2582

Open
neweldude opened this issue Mar 5, 2024 · 0 comments
Open

Extension for PHPWord/src/PhpWord/IOFactory.php::extractVariables #2582

neweldude opened this issue Mar 5, 2024 · 0 comments

Comments

@neweldude
Copy link

Is your feature request related to a problem? Please describe.

The function doesn´t get the placeholders from texts in tables.

Describe the solution you'd like

see below ;-)

Additional context

public static function extractVariables(string $filename, string $readerName = 'Word2007'): array
{
    $reader = IOFactory::createReader($readerName);
    $document = $reader->load($filename);
    $extractedVariables = [];
    foreach ($document->getSections() as $section) 
    {
        $concatenatedText = '';
        foreach ($section->getElements() as $element) 
        {
            if ($element instanceof TextRun) 
            {
                foreach ($element->getElements() as $textElement) 
                {
                    if ($textElement instanceof Text) 
                    {
                        $text = $textElement->getText();
                        $concatenatedText .= $text;
                    }
                }
            }
            
            else if ($element instanceof Table) 
            {
                foreach ($element->getRows() as $tableRows) 
                {
                    if ( $tableRows instanceof Row )
                    {
                        foreach ( $tableRows->getCells() as $tableCells )
                        {
                            if ( $tableCells instanceof Cell )
                            {
                                foreach ($tableCells->getElements() as $textrunElement) 
                                {
                                    if ($textrunElement instanceof TextRun)
                                    {
                                        foreach ($textrunElement->getElements() as $textElement)
                                        {
                                            if ($textElement instanceof Text)
                                            {
                                                $text = $textElement->getText();
                                                $concatenatedText .= $text;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        preg_match_all('/\$\{([^}]+)\}/', $concatenatedText, $matches);
        if (!empty($matches[1])) {
            foreach ($matches[1] as $match) {
                $trimmedMatch = trim($match);
                $extractedVariables[] = $trimmedMatch;
            }
        }
    }
    
    return $extractedVariables;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant