get everything between specific string php

46 Views Asked by At

I have a string of file_get_contents() which I simplify structures as follows:

$string = '
        welcome in
    admin panel
    //start>license[version(lite)]
        lite 
    //end>license[version(lite)]
    //start>license[version(professional)]
        professional
    //end>license[version(professional)]
    //start>license[version(enterprise)]
        enterprise
        //start>license[custom(company1)]
            + custom company1
        //end>license[custom(company1)]
        //start>license[custom(company2)]
            + custom company2
        //end>license[custom(company2)]
    //end>license[version(enterprise)]
    version
        1.4.2
';

I want to generate a final result like this

        welcome in
    admin panel
        professional
    version
        1.4.2

I have tried to create a function to make logic for searching [version(professional)] like this:

    $lines = explode("\n", $string);
    $output = '';
    $insideLicense = false;

    foreach ($lines as $line) {
        if (strpos($line, "//start>license[version(professional)]") !== false) {
            $insideLicense = true;
            continue;
        }

        if (strpos($line, "//end>license[version(professional)]") !== false) {
            $insideLicense = false;
            continue;
        }

        $output .= $line . "\n";
    }

    dd($output);

but my result is:

        welcome in
    admin panel
    //start>license[version(lite)] <-- remove this
        lite <-- remove this
    //end>license[version(lite)] <-- remove this
        professional
    //start>license[version(enterprise)] <-- remove this
        enterprise <-- remove this
        //start>license[custom(company1)] <-- remove this
            + custom company1 <-- remove this
        //end>license[custom(company1)] <-- remove this
        //start>license[custom(company2)] <-- remove this
            + custom company2 <-- remove this
        //end>license[custom(company2)] <-- remove this
    //end>license[version(enterprise)] <-- remove this
    version
        1.4.2

I want the <-- remove this to be removed, and the final result is:

        welcome in
    admin panel
        professional
    version
        1.4.2

can you help me, please? thank you...

2

There are 2 best solutions below

0
hirohito On

I finally discovered the solution, add more logic like this:

    if (strpos($line, "//start>license[version(") !== false) {
        $insideOtherLicense = true;
        continue;
    }
    if (strpos($line, "//end>license[version(") !== false) {
        $insideOtherLicense = false;
        continue;
    }

    if (!$insideOtherLicense) {
        $output .= $line . "\n";
    }

and my full logic is like this:

$lines = explode("\n", $string);
$output = '';
$insideLicense = false;
$insideOtherLicense = false;

foreach ($lines as $line) {
    if (strpos($line, "//start>license[version(profesional)]") !== false) {
        $insideLicense = true;
        continue;
    }
    if (strpos($line, "//end>license[version(profesional)]") !== false) {
        $insideLicense = false;
        continue;
    }

    
    if (strpos($line, "//start>license[version(") !== false) {
        $insideOtherLicense = true;
        continue;
    }
    if (strpos($line, "//end>license[version(") !== false) {
        $insideOtherLicense = false;
        continue;
    }

    if (!$insideOtherLicense) {
        $output .= $line . "\n";
    }
}

dd($output);

or if you guys have a simpler solution, please let me know that.

0
Ashis Kumar On

Here is the compact solution.

$lines = explode("\n", $string);
$replace = ['lite', 'enterprise', '+ custom company1', '+ custom company2'];
$newString = '';
foreach($lines as $line){
    if(false === strpos($line, "//start>license") && false === strpos($line, "//end>license")){
        if(!in_array(trim($line), $replace)){
            $newString .= $line;
        }
    }
}

echo $newString;