如何从PHP中的字符串中提取随机子字符串?
我想从字符串中提取随机子字符串,我该怎么做?
假设我有这个文本
$text =“我有一些完全随机的文本,我想对其进行处理!但是需要 一些 php 技能...";
因此我想要一个像这样的带有提取子字符串的数组
$random_string[0] = "random text I have";
$random_string[1] = "I have, and I want to work";
$random_string[2] = "need some php skillz...";
I would like to extract random substrings form a string, how could I do this?
Lets say I have this text
$text = "Some totally random text I have, and I want to work on it! But need
some php skillz...";
and as a result I want an array like this with extracted substrings
$random_string[0] = "random text I have";
$random_string[1] = "I have, and I want to work";
$random_string[2] = "need some php skillz...";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建小于字符串长度的两个随机数
然后使用
$n
创建子字符串希望这有帮助
更新:
你可以这样做像这样获取单词,
使用
explode
- 返回一个字符串数组,每个字符串都是字符串的子字符串$pieces 将是字符串的独立单词数组
示例:
然后使用
array_rand
- 从数组中选择一个或多个随机条目,这样
$rand_words
将从字符串中随机获得两个单词示例:
Create two random numbers which is less than the length of the string
Then create a sub string using
$n
Hope this helps
Updated:
You can do like this to get words ,
The use
explode
- Returns an array of strings, each of which is a substring of string$pieces will be a array of sepearte words of your string
Example :
Then use
array_rand
— Pick one or more random entries out of an arrayso
$rand_words
will have two random words from your stringExample :
使用具有随机开始和长度的 substr 。
如果需要,调整您的值并添加更多逻辑。
Use substr with random start and lengths.
Adjust your values and add more logic if you need.