如果您只想限制输出及其每次应该停止执行的相同字符串,请执行以下操作:
<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>
说明:
如果$texts->info()['description'] 包含文本From Account,它将通过break 结束foreach 循环的执行。如果您需要检查多个关键字read this。
另一种解决方案是在将图像发送到 API 之前使用 imagecrop() 裁剪图像。但为此,您需要确保它永远不会改变文本的大小/位置。
附:你确定每个人都应该在你的截图中看到这些私人数据吗?
更新1
如你所问。这将是相同的代码,但使用 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>
也许这可以解决您的问题,因为同一页面包含此注释:
不支持在同一控制块中混合语法。
更新2
在您更新了您的问题后,它现在更清楚了。每个文本行的输出不包含一个元素。相反,它包含多行文本。因此,我的第一个代码没有回显任何内容,因为它在第一个数组元素中找到了 From Account。
因此我们需要搜索字符串From Account 并剪切文本行:
<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>
您可以选择在<?php endforeach ?> 之前添加它以跳过所有以下数组元素:
<?php
if ($pos !== false) {
break;
}
?>
注意: @TerryLennox 使用 preg_match 来查找 From Account。这与使用strpos (most prefer avoiding regex) 没有区别。但他的回答包含另一个很好的提示。他使用文本位置信息将文本逐行添加到新数组中。这可能非常有用,具体取决于您的目标如何显示/存储文本。