Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spilt array values into separate variables

I’m trying to read my data from a txt file line by line, and if the line matches $PageName stop and spit that string into variable’s I’m a nube and cant figure it out. Here is were I’m at with my code.

$PageName = "Home";
$SiteAttributes = file("SiteAttributes.dat");

foreach($SiteAttributes as $line){
    $str_arr = preg_split ("/\|/", $line,);
    echo"$PageName $line<br>";
    if($PageName == $line[0]){
        echo"$PageName $line";
        last;
    }   
}

SiteAttributes.dat looks like this:

Amenities||||||
FloorPlans||||||
Home||This is the updatede title|this it the new Keywords|this it the new Desription|<script src="code/system/library/bx-slider/js/jquery-3.1.1-min.js">,</script><script src="code/system/library/bx-slider/js/jquery.bxslider.js"></script>,<link href="code/system/library/bx-slider/css/jquery.bxslider.css" rel="stylesheet">|
LocalMap|||||| 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Changing your file as little as possible:

$PageName = "Home";
$SiteAttributes = file("SiteAttributes.dat");

foreach($SiteAttributes as $line){

    $str_arr = preg_split ("/\|/", $line);

    if($PageName == $str_arr[0])  {
        echo "found $PageName $line";
        }

}

basically, just a simple mistake of using line as an array instead of the array you made from it. Also, not sure what the ‘last’ was for, as it’s not a keyword. And I removed the first echo since it confuses the output, makes it hard to see when you have a match.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading