javascript - 在数组中指定的单词之后添加
问题描述
我想
在我在数组中定义的单词之后添加。所以我希望它出来: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]+' ');
}
解决方案
尝试 :
$search = array('w ', 'z ', 'u ', 'o ', 'ul. ');
$replace = array('w ', 'z ', 'u ', 'o ', 'ul. ');
$titleOld = "Testing w it's ul. here";
$title = str_replace($search, $replace, $titleOld);
(https://www.php.net/manual/fr/function.str-replace.php,第一个例子,第三种情况)
推荐阅读
- django - 我可以将单个 Django 模型字段拆分为多个 Django Rest Framework 序列化器字段吗?
- git - Git fetch - 它是否获取所有分支?
- swift - 如何在 NavigationLink 中有多个目的地
- angular - Angular 9 时区 DatePipe
- angular - 使用 angular ivy 创建真正的插件架构
- javascript - 烧瓶上传多个文件
- django - 如果用户未通过 django 身份验证,则显示 404 页面
- python - 在 datalab 中导入 gcsfs 出现错误
- html - 根据高度动态调整图像
- c# - 登录后显示用户信息