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|||||| 

>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.

Leave a Reply