read string between two double quote php

good day, I have a UML class diagram exported to an XML file, and the code basically and repeatedly is like this:

<UML:Class name="Zip Code" isLeaf="false" xmi.id="{C7C65474-DD51-4165-89A0-FB552A929185}" isAbstract="false" visibility="public">

So, what i need to do, is to output to the user all the class(etc) found in the file. More exactly, i need to read the name inside the double quotes.

More properly, read the string inside the double quotes from a specific tag, example:

to get all classes names i must search in: <UML:Class name=" STRING WHAT I WANNA GET "
to get all atributtes names i must search in: <UML:Attribute name=" STRING I WANNA GET "

i have this code, but when i run it, it show me all the line where it found the code <UML:Class name= etc >

[php]
$file = ‘order.xml’;
$searchfor = ‘UML:Attribute name="’;
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, ‘’);
$pattern = “/^.$pattern.$/m”;

if(preg_match_all($pattern, $contents, $matches)){
echo “Found matches:\n”;
echo implode("\n", $matches[0]);
}
else{
echo “No matches found”;
}
[/php]

i dont know if i could do this away, or if i have to use the xml parser

thanks all

I made a test page for you including a function to do what you want at the top.

Hopefully the example usages will make it easy to see how it’s used, but if not let me know and I’ll try to clarify more. Also let me know if you want any explanation of the code actually inside the function (or would like any of it changed). I’ve just written it how I feel should be flexible and useful, but your needs are as yet unknown to me, so just shout if you need anything!

FILE SRC:
[php]<?php
/*

  • Debug function for viewing output of arrays
    */
    function pre($array,$note=’’)
    {
    if(trim($note) != ‘’) $note .= ’ ';
    echo ‘
    ’.$note;
    print_r($array);
    echo ‘
    ’;
    }

/**

  • This function reads the XML formatted data in $raw_data, and iteratively goes through the $requests finding the requested data
  • @param string $raw_data A string containing the XML format data
  • @param array $requests An array of request arrays: array(array(‘group’ => ‘attrib’),array(‘group2’ => ‘attrib’)) or one request array: array(‘group’ => ‘attrib’)
  • @param bool $case_sensitive Whether or not the group/attribute names should be dealt with in a case-sensitive manner
  • @return array An array, wherein each element contains an error string if one occurred, and the value of the requested attribute
    */
    function get_attribs($raw_data,$requests,$case_sensitive=false)
    {
    $return = array();
    $single = false;
    if(is_array($requests) && count($requests) == 1) //Passed with only one request: Array(‘Class’ => ‘name’)
    {
    $single = true;
    $requests = array($requests);
    }
    foreach($requests as $request)
    {
    $group = key($request);
    $attrib = $request[$group];
    $regexp = ‘#<[^:]+:’.preg_quote($group).’[^>]+(’.preg_quote($attrib).’)="([^"]+)"#’;
    if($case_sensitive !== true) $regexp .= ‘i’;
    preg_match($regexp,$raw_data,$data);
    if(isset($data[1])) $attrib = $data[1];//Get actual attribute name for situations where case is wrong
    if(!isset($data) || empty($data) || !isset($data[2]))
    {
    $return[$group.’:’.$attrib] = array(
    ‘error’ => ‘Attribute value could not be found.’
    ,‘value’ => ‘’
    ,‘group’ => $group
    ,‘attribute’ => $attrib
    );
    }
    else
    {
    $return[$group.’:’.$attrib] = array(
    ‘error’ => ‘’
    ,‘value’ => $data[2]
    ,‘group’ => $group
    ,‘attribute’ => $attrib
    );
    }
    }
    if($single === true) return array_shift($return);
    return $return;
    }

$test_string = ‘<UML:Class name=“Class Name” isLeaf=“false” xmi.id="{C7C65474-DD51-4165-89A0-FB552A929185}" isAbstract=“false” visibility=“public”>
<UML:Attribute name=“Attrib name” isLeaf=“false” xmi.id="{C7C65474-DD51-4165-89A0-FB552A929185}" isAbstract=“false” visibility=“public”>
<UML:Another name=“name of another” isLeaf=“false” xmi.id="{C7C65474-DD51-4165-89A0-FB552A929185}" isAbstract=“false” visibility=“public”>’;
?>

<?php pre(get_attribs($test_string,array('Class' => 'isleaf'))); //First output
	$requests = array();
	$requests[] = array('Class' => 'name');
	$requests[] = array('Class' => 'isabstract');
	$requests[] = array('Another' => 'xmi.id');
	
	pre(get_attribs($test_string,$requests)); //Second output
	?>
</body>
[/php]

OUTPUT:

[code]Array
(
[error] =>
[data] => false
[group] => Class
[attribute] => isLeaf
)

Array
(
[Class:name] => Array
(
[error] =>
[data] => Class Name
[group] => Class
[attribute] => name
)

[Class:isAbstract] => Array
    (
        [error] => 
        [data] => false
        [group] => Class
        [attribute] => isAbstract
    )

[Another:xmi.id] => Array
    (
        [error] => 
        [data] => {C7C65474-DD51-4165-89A0-FB552A929185}
        [group] => Another
        [attribute] => xmi.id
    )

)
[/code]

I am very happy for an answer / help, so thank you so much. :slight_smile:
i will try to clarify more what i am doing, and where this problem exists to clarify all, from my perspective and from your final code.

This issue above, is a project that I have to perform in stage, and its functionality and purpose would be to convert an XMI file to a SQL file. But this project was not fully accompanied by teachers in order to satisfy my doubts and my colleague), it is now impossible to finish the project itself.
so basically the project contained three parts: the first (my part) would be the construction of the web platform to perform the conversion itself, the second (the part of my colleague / problem reported here on the forum) would collect the most important information of the XMI file, and the last would be together, and that was the development of code to create the SQL file using the important information extracted from the XMI file.

The XMI files will be similar to this one:

http://www.dragbox.org/f/lsfb3

So what should happen was:
1 - The user uploads the XMI file
2 - The php checks and extracts the most important information of the file
3 - Information is shown on the screen

The most important information should be presented as follows:
As you could see in the code (check link), there are attributes in each class, for example in the class “Colaborator” there is “Colaborator Name” and “Colaborator Address”.
So what I need is something that search the file if there are classes, if any, should seek the attributes, and if they have attributes, it should organize / display information organized by class name.
example:

Class (Colaborator)
Atributes “Colaborator Name”, “Address Colaborator”

Class (ZipCode)
Atributes: “Zip Code ID”, “Location”

If you’ve read this far, thank you and hope you can help me :slight_smile:
ps: sorry for so long text :smiley:

Consider using SimpleHTMLDOM which should work well with XML files as well. it should take like 5 minutes to understand the basics, which from your post sounds exactly what you need.

You could try something like that, but this function is only 20 lines and should get you the data you requested - if it doesn’t let me know. (see output below)

[php]function parse_xmi($xmi_data) //$xmi_data can be the string of contents or a file path
{
if(is_file($xmi_data) && is_readable($xmi_data)) $xmi_data = str_replace(array("\t","\r\n","\r","\n"),’’,file_get_contents($xmi_data)); //strips out unnecessary characters
$end = strlen($xmi_data);

$return = array();
$matches = array();
$last = 0;
while(strpos($xmi_data,'<UML:Class name="',$last) !== false)
{
	$pos = strpos($xmi_data,'<UML:Class name="',$last);
	preg_match('/<UML:Class name="([^"]+)"/',substr($xmi_data,$pos-1,150),$matched);
	$matches[] = array('pos' => $pos,'class' => $matched[1]);
	$last = $pos+1;
}


foreach($matches as $key => $data)
{
	$pos = $data['pos'];
	$class = $data['class'];
	$next = isset($matches[$key+1]) ? $matches[$key+1]['pos'] : $end;
	
	$thisclass = trim(substr($xmi_data,$pos,$next-$pos));
	
	preg_match_all('#<UML:Attribute name="([^"]+)"#',$thisclass,$found);
	
	$return[$class] = $found[1];
}

return $return;

}

$result = parse_xmi(‘m1ke4fun.xml’);

echo ‘

’;
print_r($result);
echo ‘
’;[/php]

The output:

[code]Array
(
[Colaborator] => Array
(
[0] => Colaborator Name
[1] => Colaborator Address
)

[Zip Code] => Array
    (
        [0] => Zip Code ID
        [1] => Location
    )

[Purchase Order] => Array
    (
        [0] => PO Number
        [1] => Amount
        [2] => Date
    )

[Withhold Request] => Array
    (
        [0] => Date
        [1] => Amount
    )

[Budget Reaagement Request] => Array
    (
        [0] => Date
        [1] => Amount
    )

[Minute Meeting] => Array
    (
        [0] => Minute ID
        [1] => Data
        [2] => Text
    )

)[/code]

Then you can just loop through each element like:
[php]if(!empty($result))
{
foreach($result as $class => $attributes)
{
echo ‘’.$class.’’;
echo ‘

    ’;
    foreach($attributes as $attrib)
    {
    echo ‘
  • ’.$attrib.’
  • ’;
    }
    echo ‘
’;
echo ‘
’;
}
}[/php]

thank you very much for the help you give to me and to other people in this forum, you are a great man :slight_smile: :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service