How to exit from an else if when condition met

I have the Foreach loop as below and it is working fine. I want to exit from the elseif when a value is found. I tried changing the elseif to a while and promptly ran out of memory. I assume that there is a way to do this but to date I have been unable to find it.

[php]
foreach ($lines as $line)
{
if (preg_match("/^0 @\w+@ \INDI/", $line))
{
$person[] = $line;
$i++;
$storeLines = true;
}
elseif ($storeLines)
{
$recd[$i][] = $line;
}
}
[/php]

What I want is

[php]
if (strpos($line,-3) ==‘FAM’) //end the elseif if this is true
[/php]

break

Thanks.

A follow on that works if I give it the full content of $line but does not work using strpos even though checking the string the last 3 characters are indeed FAM. I can’t use the full value of $line as I cannot guarantee the content of the string apart from the last 3 characters.

This is what I have that doesn’t work

[php]elseif ($storeLines)
{
if (strpos($line,-3) == ‘FAM’){break;}
//if ($line == ‘0 @F1@ FAM’){break;}
$recd[$i][] = $line;
}[/php]

If I put in the alternative as above it does work

strpos returns an index, not a string, so it would never be FAM.

preg_match Is possibly a better route to take for searching thru a line of specific characters.

:smiley:

Great thanks that works perfectly.

Sponsor our Newsletter | Privacy Policy | Terms of Service