If you only want to limit the output and its every time the same string that should stop the execution, then do the following:
<div class="row">
<div class="col-12">
<ol>
<?php foreach ($text as $key => $texts): ?>
<?php if (strpos($texts->info()['description'], 'From Account') !== false) break; ?>
<li><h6> <?php echo ucfirst($texts->info()['description']) ?></h6><<br><br>
</li>
<?php endforeach ?>
</ol>
</div>
</div>
Explanation:
If $texts->info()['description']
contains the text From Account
it ends the execution of the foreach loop through break
. If you need to check for multiple keywords read this.
An alternative solution would be to crop the image with imagecrop()
before sending it to the API. But for this you need to be sure that it never changes the size / position of the texts.
P.S. are you sure everyone should see those private data in your screenshot?
Update1
As you asked. This would be the same code but using the alternative syntax for control structures:
<div class="row">
<div class="col-12">
<ol>
<?php foreach ($text as $key => $texts): ?>
<?php if (strpos($texts->info()['description'], 'From Account') !== false): ?>
<?php break; ?>
<?php endif; ?>
<li><h6> <?php echo ucfirst($texts->info()['description']) ?></h6><<br><br>
</li>
<?php endforeach ?>
</ol>
</div>
</div>
Maybe this solves your problem as the same page includes this note:
Mixing syntaxes in the same control block is not supported.
Update2
After you updated your question its more clear now. The output does not contain one element per text line. Instead it contains multiple lines of texts. Because of that my first code did not echo anything as it finds From Account
in the very first array element.
Because of that we need to search for the string From Account
and cut the text line:
<div class="row">
<div class="col-12">
<ol>
<?php foreach ($text as $key => $texts): ?>
<?php
$text = $texts->info()['description'];
// search for string
$pos = strpos($texts->info()['description'], 'From Account');
if ($pos !== false) {
// if the string was found cut the text
$text = substr($text, 0, $pos);
}
?>
<li><h6> <?php echo $text ?></h6><<br><br>
</li>
<?php endforeach ?>
</ol>
</div>
</div>
Optionally you could add this before <?php endforeach ?>
to skip all following array elements:
<?php
if ($pos !== false) {
break;
}
?>
Note: @TerryLennox uses preg_match
to find From Account
. There is no difference between this and using strpos
(most prefer avoiding regex). But his answer contains another good tip. He uses the text position information to add the text line by line to a new array. This could be really useful depending on your targets how to display/store the text.