gpt4 book ai didi

php - 从字符串中删除不必要的单词

转载 作者:行者123 更新时间:2023-12-03 20:51:02 25 4
gpt4 key购买 nike

我想从字符串中删除一些单词,以使我的图像 alt 标记更相关。

问题是,如果我有这样的阶段:

barack obama at a restaurant in venice

我希望导出是:

barack obama restaurant venice

但我拥有的是:

barack obamaa restaurantvenice

代码:-

$a = array( ' at ', ' in ', ' arriving ', ' a ', ' leaving ', ' walks ', ' walking ', ' her ' );
$str = 'barack obama at a restaurant in venice';
echo str_replace($a,"",$str);

如果我使用数组中没有空格的单词,结果会非常可怕:

brck obm resturnt venice

有什么想法吗?

最佳答案

您必须在""内添加一个空格,如下所示:-

echo str_replace($a," ",$str);

检查这里:-

<?php

$a = array( ' at ', ' in ', ' arriving ', ' a ', ' leaving ', ' walks ', ' walking ', ' her ' );
$str = 'barack obama at a restaurant in venice';
echo str_replace($a," ",$str);

输出:- https://eval.in/636890

关于php - 从字符串中删除不必要的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39368637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com