具有一个输出的多阵列

时间:2016-05-24 13:34:58

标签: php arrays

这是我的代码,目前无法正常运行。我怎样才能让它发挥作用?我希望输出像一个字符串(当然我知道如何“转换”数组到字符串):

  

更改,添加和删除单词以使其成为

代码:

!public/lib/jquery/jquery.js

输出:

public/lib/**/*.js

3 个答案:

答案 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);
  • 通过在交替(OR)运算符|
  • 上插入单词来创建模式
  • 将找到的单词替换为单词$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;
}