Jump to content

Can't extract required result


x1nick

Recommended Posts

Hi

 

I have different variables

{include file.php}

{include admin file.php}

{include temp file.php}

 

What i need from the regex is for it to return the 'admin' (or what ever it might be) from the variable separately into the array

 

This is my current bit of code im experimenting with

$data = "{include file.php}
{include admin file.php}
{include temp file.php}";

preg_match_all('/{include (?:admin([^}]*))?([^}]*)}/Uis', $data, $matches);
echo '<pre>';
print_r($matches);

 

I have only put admin in there as im unsure on how to do this correctly

 

Currently the array looks like

Array
(
    [0] => Array
        (
            [0] => {include file.php}
            [1] => {include admin file.php}
            [2] => {include temp file.php}
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [2] => Array
        (
            [0] => file.php
            [1] => admin file.php
            [2] => temp file.php
        )

)

 

What I want it to look like:

Array
(
    [0] => Array
        (
            [0] => {include file.php}
            [1] => {include admin file.php}
            [2] => {include temp file.php}
        )

    [1] => Array
        (
            [0] => 
            [1] => admin
            [2] => temp
        )

    [2] => Array
        (
            [0] => file.php
            [1] => file.php
            [2] => file.php
        )

)

 

Hope that makes sense :)

Link to comment
Share on other sites

One quick possible solution could be:

 

$data = "{include file.php}
{include admin file.php}
{include temp file.php}";

preg_match_all('#{include ([a-z]+ )?([a-z]+\.[^}]+)}#i', $data, $matches);
echo '<pre>'.print_r($matches, true);

 

If there could be stuff like underscores, numbers or what have you, simply add them into the character classes. This all makes rigid assumptions about spacing / formating (like a single space delimiting each set of data).

Link to comment
Share on other sites

yeah I noticed that after I posted. figured it wasn't worth going back to edit, that he should probably be able to figure that out on his own. I hate going back to edit. aside potentially causing confusion, I feel it somehow cheapens my post on some level.  This, coming from a guy who shamelessly spams like a 13 year old script kiddie with mommy issues.

Link to comment
Share on other sites

Hi thanks for the input, works perfectly  :D

 

Got another problem with regex, although this one more likely is the way im doing things.

 

I have the following

 

{permission abc} {/permission}

 

What I want to be able to do is have another {permission nested inside (and possibly another and so on), like this

 

{permission abc}

    {permission def}

 

    {/permission}

{/permission}

 

If this at all possible with regex?

 

Link to comment
Share on other sites

regex is difficult at best when dealing with nested tags like that.  If you know exactly how many times something can be nested, you can with much effort use regex to parse it.  But if it can be nested any number of levels deep, don't bother trying to do it with regex.  I suggest you look into redoing your tag syntax and using an xml or dom parser class.

Link to comment
Share on other sites

Hi

 

They are being replaced with php

 

if ($cms->permission('###permission value###') {

 

 

and closing

{/permission}

 

}

 

 

I might look at doing something like that, and counting the number of opens to the number of closes to try calculate if it contains all the rightclosing points.

 

Unless there could be a better way

 

Suppose I could do a search for the opening tag, then once that's found look for a closing tag. If both are found then replace them.

Link to comment
Share on other sites

When it's building PHP, I think the code should spit out an error if the tags don't match up, as automatically adding or removing closing tags to make them match would create unexpected results in most cases. Here's a simple example:

 

<?php
//$source contains the source string
$temp = preg_replace('~{permission\s+([^}]+)}~i', 'if ($cms->permission(\'$1\')) {', $source, -1, $opening_count);
$temp = str_ireplace('{/permission}', '}', $temp, $closing_count);
if ($opening_count === $closing_count) {
//if a matching number of tags were replaced, overwrite $source with the modified string
$source = $temp;
} else {
//error
trigger_error('Opening and closing tags don\'t match up', E_USER_ERROR);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.