Question:
How to extract tags from TMX (XML) content using PHP

To extract tags from a Translation Memory Exchange (TMX) file in PHP, you can use the SimpleXML extension, which allows easy manipulation of XML data. TMX files typically contain translation units with source and target language pairs, and each translation unit may have additional metadata or tags.



The issue seems to come from objectsIntoArray(), and not having the code makes it difficult to fix.

If you remove that call and instead use the SimpleXML elements as they are intended to be used, you can call it using (other code removed to focus on this issue)...


<?php

// Load the TMX file into SimpleXML

$tmxFile = 'path/to/your/file.tmx';

$xml = simplexml_load_file($tmxFile);


// Check if the XML was loaded successfully

if ($xml === false) {

    die('Error loading TMX file.');

}


// Loop through translation units

foreach ($xml->body->tu as $tu) {

    // Extract source and target segments

    $sourceSegment = (string)$tu->tuv[0]->seg;

    $targetSegment = (string)$tu->tuv[1]->seg;


    // Extract tags

    $sourceTags = extractTags($sourceSegment);

    $targetTags = extractTags($targetSegment);


    // Output tags for each translation unit

    echo "Source Tags: " . implode(', ', $sourceTags) . PHP_EOL;

    echo "Target Tags: " . implode(', ', $targetTags) . PHP_EOL;

}


/**

 * Function to extract tags from a segment.

 *

 * @param string $segment

 * @return array

 */

function extractTags($segment) {

    $tags = [];

    preg_match_all('/<([^>]+)>/', $segment, $matches);

    if (!empty($matches[1])) {

        $tags = $matches[1];

    }

    return $tags;

}


Credit:> StackOverflow


Suggested blog:

>Guide: Building a responsive Flutter Web- Part 1

>Firebase Authentication in Flutter

>Firebase Authentication using Provider in Flutter

>Build an Animated App with Rive and Flutter

>How to start programming for Artificial Intelligence?

>How to implement AI and ML with .NET Applications

>Quality Testing in AI: Manual to Autonomous Testing

>Top 8 programming languages to used in artificial intelligence coding

>How you can pick a language for AI programming

>Supervised Learning vs Unsupervised Learning- Technical Chamber


Submit
0 Answers