Retrieve specific information in string

Asked

Viewed 42 times

-2

I am developing a tool that collects from a string only the ID fields and the name. However I do not know how to do this in php follows below the string.

<s:Label x="10" y="10" text="NOME"/>
                <s:TextInput id="NOME" x="10" y="21" width="290" tabIndex="1"/>
                <s:Label x="10" y="50" text="TELEFONE"/>
                <s:Label x="120" y="50" text="RAMAL"/>
                <s:TextInput id="RAMAL" x="120" y="61" width="102" tabIndex="5"/>
                <s:Label x="365" y="50" text="LOGIN"/>
                <s:TextInput id="LOGIN1" x="365" y="61" width="102" tabIndex="7"/>
                <s:TextInput id="TELEFONE" x="10" y="61" width="102" tabIndex="4"/>
                <s:Label x="308" y="11" text="EMAIL"/>
                <s:TextInput id="EMAIL" x="308" y="22" width="290" tabIndex="2"/>
                <s:Label x="605" y="10" text="MATRICULA"/>
                <s:TextInput id="MATRICULA" x="606" y="22" width="144"  tabIndex="3"/>
                <s:Label x="627" y="91" text="NASCIMENTO"/>
                <s:TextInput id="NASCIMENTO" x="627" y="103" width="121" tabIndex="14"/>

Note that in each object except the label there is a precise ID to pick up the contents inside the simple quotes example: id="NAME"

How to do this? Remembering that this string can be gigantic

  • This is all a string, with line breaking and everything?

  • Yeah, and I just need to get the contents of the Ids

  • 2

    I believe that a preg_match_all('/id="(.+?)"/', $str, $matches); var_dump($matches[1]); resolves. The variable $str is where the string is stored.

  • Boy you’re a genius That’s right

2 answers

2

Can use preg_match_all with a regular expression to pick up anything between double quotes of id="":

preg_match_all('/id="(.+?)"/', $str, $matches);

The expression /id="(.+?)"/ will fetch everything you have id="" and the part (.+?) separates in group 1 everything that is between the quotes. The result is an array with two indexes ([0] and [1]) where in the index [0] is the complete standard (id="alguma coisa") and in the index [1] what’s between the quotes.

Therefore, you only care about the content [1]:

$matches[1]

Check on IDEONE

2

This string seems to me to be a clipping of an XML, but it is badly formed (missing some parts).

You can read this string with the Simplexml:

<?php

$xml = <<<'XML'
<root xmlns:s="http://some-address.com">
    <s:Label x="10" y="10" text="NOME"/>
    <s:TextInput id="NOME" x="10" y="21" width="290" tabIndex="1"/>
    <s:Label x="10" y="50" text="TELEFONE"/>
    <s:Label x="120" y="50" text="RAMAL"/>
    <s:TextInput id="RAMAL" x="120" y="61" width="102" tabIndex="5"/>
    <s:Label x="365" y="50" text="LOGIN"/>
    <s:TextInput id="LOGIN1" x="365" y="61" width="102" tabIndex="7"/>
    <s:TextInput id="TELEFONE" x="10" y="61" width="102" tabIndex="4"/>
    <s:Label x="308" y="11" text="EMAIL"/>
    <s:TextInput id="EMAIL" x="308" y="22" width="290" tabIndex="2"/>
    <s:Label x="605" y="10" text="MATRICULA"/>
    <s:TextInput id="MATRICULA" x="606" y="22" width="144"  tabIndex="3"/>
    <s:Label x="627" y="91" text="NASCIMENTO"/>
    <s:TextInput id="NASCIMENTO" x="627" y="103" width="121" tabIndex="14"/>
</root>
XML;

$simpleXML = new SimpleXMLElement($xml);
$xpathTextInput = $simpleXML->xpath('s:TextInput');

foreach($xpathTextInput as $input) {
    echo $input['id'] . PHP_EOL;
}

Upshot:

NOME
RAMAL
LOGIN1
TELEFONE
EMAIL
MATRICULA
NASCIMENTO

I changed a little to make XML valid (you need a root tag and also declare the namespace). See working here.

See some references here:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.