【问题标题】:Add   after word specified in array在数组中指定的单词之后添加
【发布时间】:2021-04-02 10:44:02
【问题描述】:

我想在我在数组中定义的单词之后添加 。所以我希望它出来:Testing w it's ul. here。请帮忙。我不知道该怎么做。

$array = array('w','z','u','o','ul.');
$titleOld = 'Testing w it's ul. here';
$title = str_replace($array.' ',' ',$titleOld);

编辑:我做这样的事情:

$nbspBefore = array(' w ',' z ',' u ',' o ',' ul. ');
$nbsp = array(' w ',' z ',' u ',' o ',' ul. ');
$title = 'Testing w it's ul. here';
$title = str_replace($nbspBefore,$nbsp,$title);

我认为这不是干净的代码,但它工作正常。

编辑 2:好的,我用另一种方式,在 JS 中。

var array=['w','z','u','o','ul.'],
    text = document.querySelector('selector');
for(i=0;i<array.length;i++){
    text.innerHTML = text.textContent.replace(new RegExp(' '+array[i]+' ','g'),' '+array[i]+'&nbsp;');
}

【问题讨论】:

标签: javascript php arrays


【解决方案1】:

需要将这种格式的数组放入str_replace()

$array = ['word1' => 'word1&nbsp;', 'word2' => 'word2&nbsp;'];

你可以这样得到:

$words = ['word1', 'word2'];
$array = array_combine($words, $words); // ['word1' => 'word1', 'word2' => 'word2']
$finalArray = array_map(fn ($word) => $word . '&nbsp;', $array); // ['word1' => 'word1&nbsp;', 'word2' => 'word2&nbsp;']

$title = str_replace($finalArray, $titleOld);

【讨论】:

  • str_replace() 接受 3 个参数,而不是 2 个。结果将是 "Testing w it's u l. here"
【解决方案2】:

您可以按空格分割字符串,并检查每个世界是否与给定数组匹配。

$array = array('w','z','u','o','ul.');
$titleOld = 'Testing w it\'s ul. here';

$words = explode(' ', $titleOld);
$titleNew = trim(join('', array_map(function($word) use ($array) {
    return $word . (in_array($word, $array) ? "&nbsp;" : ' ');
}, $words)));

echo $titleNew; // "Testing w&nbsp;it's ul.&nbsp;here"

或者,从 PHP 7.4 开始:

$titleNew = trim(join('', array_map(fn($word) => 
    $word . (in_array($word, $array) ? "&nbsp;" : ' '),
    $words)));

在线示例:up to 7.3as of 7.4.

【讨论】:

    【解决方案3】:

    简单的方法使用 foreach 在句子中按空格循环数组 exploded 并使用 @987654323 检查是否有任何单词与我们想要的 array 中的可替换单词匹配@ 方法,如果是则添加 &amp;nbsp; 否则就保持原样并添加一个空格。

    $array = array('w','z','u','o','ul.');
    $titleOld = 'Testing w it\'s ul. here';
    
    $words = explode(' ', $titleOld);
    $title = '';
    foreach ($words as $i=>$word) {
        if(in_array($word,$array)){
            $title.=$word.'&nbsp;';
        }else{
            if($i===count($words)-1)
                $title.=$word;
            else
                $title.=$word." ";
        }
    }
    echo $title;//Testing w&nbsp;it's ul.&nbsp;here
    
    

    更新:我在末尾有一个空格的问题,所以我通过向 foreach 添加索引来解决它,以便跟踪单词是否是最后一个,如果是,我们不应该添加空格,否则我们添加一个空格。

    【讨论】:

      【解决方案4】:

      您可以使用正则表达式让您的生活更轻松。

      
      $array = array('w','z','u','o','ul.');
      $titleOld = "Testing w it's ul. here cappuccino";
      
      array_map(function ($w) use (&$titleOld) {
      
          $titleOld = preg_replace("/\b" . $w . "\s{1}\b/", "$w&nbsp;", $titleOld);
      
      }, $array);
      
      echo $titleOld;  // Testing w&nbsp;it's ul.&nbsp;here cappuccino
      

      【讨论】:

      • 但这添加了   word中的字母是什么时候,例如:“cappuccino ”我只想在“o”本身之后
      • @test,嗯,这是因为您的规格不明确。不用担心,我已经更新了我的答案。请尝试新版本。
      【解决方案5】:

      试试:

      $search = array('w ', 'z ', 'u ', 'o ', 'ul. ');
      $replace = array('w&nbsp;', 'z&nbsp;', 'u&nbsp;', 'o&nbsp;', 'ul.&nbsp;');
      $titleOld = "Testing w it's ul. here";
      $title = str_replace($search, $replace, $titleOld);
      

      https://www.php.net/manual/fr/function.str-replace.php,第一个例子,第三种情况)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多