这是我的代码,目前无法正常运行。我怎样才能让它发挥作用?我希望输出像一个字符串(当然我知道如何“转换”数组到字符串):
更改,添加和删除单词以使其成为
代码:
!public/lib/jquery/jquery.js
输出:
public/lib/**/*.js
答案 0 :(得分:1)
如果没有更多解释,它就像这样简单:
$text = strip_tags("words altered added and removed to make it");
$words = array("altered", "added", "something");
$result = $text;
foreach($words as $word) {
$result = str_replace($word, "$word,", $result);
}
或放弃循环方法:
$text = strip_tags("words altered added and removed to make it");
$words = array("altered", "added", "something");
$result = preg_replace('/('.implode('|', $words).')/', '$1,', $text);
|
$1
和逗号答案 1 :(得分:0)
您可以使用iterator
// Array with your stuff.
$array = [];
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as $value) {
echo $v, " ";
}
答案 2 :(得分:0)
您的原始方法应该进行一些修改。转而使用爆炸字符串中的单词。对于每一个,如果它在要修改的单词数组中,请添加逗号。如果没有,不要。然后修改(或不)单词进入堆栈。
$text = explode(" ", strip_tags("words altered added and removed to make it"));
$words = array("altered", "added", "something");
foreach ($text as $word) {
$stack[] = in_array($word, $words) ? "$word," : $word;
}