0

I have XML data inside a variable.

I now need to search for a specific text inside this variable.

Can anyone point me in the right direction? I've been googling this but no good results :o/

The XML data looks like this:

<status>
<connection_status>successful</connection_status>
<operation_status>successful</operation_status>
<Options>
<data_2>
<data_7422731>
<id>7431</id>
<assetId>2</assetId>
<startDate>2013-03-05 11:00:00</startDate>
<endDate>2013-03-05 12:00:00</endDate>
</data_7422731>
</data_2>
</Options>
</status>

I need to search for a specified ID.

For example the id "7431"

5
  • Are you looking for a specific element? Or rather specific text-content? Commented Mar 5, 2013 at 11:58
  • 3
    Post your code. Show your efforts. Commented Mar 5, 2013 at 11:59
  • Can you show the code (structure of the XML file, what you are looking for and what you have tried)? Commented Mar 5, 2013 at 11:59
  • -1 for lack of clarity and lack of code Commented Mar 5, 2013 at 12:06
  • I have added the data structure. thanks Commented Mar 5, 2013 at 12:07

2 Answers 2

2

Use an XML parser to load the string to XML, then get its children or use XPATH like:

/status/Options/data_2//id
Sign up to request clarification or add additional context in comments.

Comments

1

Well this isn't quite XML related, but like others said, unless you provide more detail and code, there's not much help we can do.

This is a quick dirty fix in case you don't want to be bothered setting up an XML parser:

Edit: I updated the code to fix some tiny mistakes, and I turned it into a function so you can call it into any tag.

function findID($mystring,$tag)
{
    $begintag="<$tag>";
    $endtag = "</$tag>";
    $beginpos = strpos($mystring, $begintag);
    $endpos = strpos($mystring,$endtag );

    $length = $endpos - ($beginpos + strlen($beginpos) +1);

    $extractedvariable = substr($mystring, $beginpos + strlen($beginpos) +1, $length); 

    return $extractedvariable;
}

Usage example:

$data='<status>
            <connection_status>successful</connection_status>
            <operation_status>successful</operation_status>
            <Options>
            <data_2>
            <data_7422731>
            <id>7431</id>
            <assetId>2</assetId>
            <startDate>2013-03-05 11:00:00</startDate>
            <endDate>2013-03-05 12:00:00</endDate>
            </data_7422731>
            </data_2>
            </Options>
            </status>';

echo findID($data,'id');    

I still recommend using an XML parser if you are going to use most of the tags in the variables or if you have multilevel XML.

1 Comment

It return blank ... what I want to do is tell it: "find text '7431' in $data variable"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.