PHP 用列表中的随机单词替换单词?
使用 PHP,我希望获取一段文本并搜索它,并将某些单词替换为列表中的另一个单词。
例如,
搜索文本以查找此列表中的任何单词: 漂亮、美丽、华丽、可爱、有吸引力、吸引人
,然后将此单词替换为同一列表中的另一个单词 (但不选择相同的单词)。
希望这是有道理的!
提前致谢。
Using PHP, I'm looking to take a piece of text and search through it and replace certain words with another word from the list.
e.g.
Search through the text to find any word in this list:
pretty,beautiful,gorgeous,lovely,attractive,appealing
and then replace this word with another from the same list
(but not selecting the same word).
Hope this makes sense!
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
preg_replace_callback
:来确保您的
$needle
数组不包含在正则表达式中具有特殊含义的字符,我们称之为preg_quote
在搜索之前对数组的每个项目进行分析。除了执行
do{}while()
循环,您还可以复制数组并删除匹配的单词(很大程度上取决于实际数据:几个项目 → 复制并删除,许多项目 → 选择一个)随机,直到与匹配的不同)you could use
preg_replace_callback
:to make sure your
$needle
array does not contain characters which have a special meaning in a regular expression, we callpreg_quote
on each item of the array before searching.instead of doing a
do{}while()
loop you could also copy the array and remove the matched word (pretty much depends on the actual data: few items → copy&remove, many items → pick one random until it's different from the match)