简体   繁体   中英

PHP new line array, look ahead

I have the following code routine, which is preg_match ing xml elements and printing the attributes of those elements.

However, in some of the tags, the content does not appear on one line (the SCRIPT tag), and therefore doesnt get matched.

I am wondering how to look ahead and gather all the lines until the closing tag "/>" ?

Is it possible to use the @ character somewhere in the preg_match, to allow new lines?

I'm not even sure about how to go about resolving this. I've done a PHP sandbox so the code can be tested online:

http://sandbox.onlinephpfunctions.com/code/f96daef33fb49179eee30250ded81af6a8e5c567

If I remove all the data in the script tag, all apart from the first line, then it correctly outputs the array.

$file = '    <TOPTAG class="Menu" text="FCLPHP" >
        <TAG1 name="contain=" />
        <SCRIPT name="check()" script="if(B3||B4||B5 == 1){
        do(ABC,0);
        do(BCD,1);" />
    </WINDOW>
';

//split the string into an array based on new line
$lines = explode("\n", $file);

//count the number of lines
$linesLength = count($lines);

for($index = 0; $index < $linesLength; $index++){

    //reads all element atrributes from the TOPTAG element
    $reads = element_attributes('TOPTAG',$lines[$index]);   

    //reads all element atrributes from the SCRIPT element
    $scripts = element_attributes('SCRIPT',$lines[$index]);

    //prints the script tag attributes
    print_r($scripts); 
}


function element_attributes($element_name, $xml) {
    if ($xml == false) {
        return false;
    }
    // Grab the string of attributes inside an element tag.
    $found = preg_match('#<'.$element_name.
            '\s+([^>]+(?:"|\'))\s?/?>#',
            $xml, $matches);
    if ($found == 1) {
        $attribute_array = array();
        $attribute_string = $matches[1];
        // Match attribute-name attribute-value pairs.
        $found = preg_match_all(
                '#([^\s=]+)\s*=\s*(\'[^<\']*\'|"[^<"]*")#',
                $attribute_string, $matches, PREG_SET_ORDER);
        if ($found != 0) {
            // Create an associative array that matches attribute
            // names to attribute values.
            foreach ($matches as $attribute) {
                $attribute_array[$attribute[1]] =
                        substr($attribute[2], 1, -1);
            }
            return $attribute_array;
        }
    }
    // Attributes either weren't found, or couldn't be extracted
    // by the regular expression.
    return false;
}

Your regexp operates across multiple lines. The problem is that you're only using it on one line at a time, so it never sees the continuation. Don't split the file into lines, just work with it as a single string.

$reads = element_attributes('TOPTAG',$file);
$scripts = element_attributes('SCRIPT',$file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM