Insert keywords to the string at random position in PHP

Source: Internet
Author: User
Keyword $ times = 4 is inserted into the string at a random position in PHP. // keyword frequency $ keyword = "[fuck ]"; // The keyword to be replaced $ str = "Brown is the right Chinese and Western Medicine Group of the aluminum sulfate group. The las will be displayed in the third part of the Chinese and Western medicine. iasfls. sdmflaskdf L. A. Law is stuck. oh, iuoi020990. open time. PHP random position. Insert a keyword to the string.

$ Times = 4; // keyword frequency
$ Keyword = "[fuck]"; // keyword to be replaced
$ Str = "Brown is staying in the aluminum sulfate group, right in the Chinese and Western Medicine 3. the las region is determined. iasfls. sdmflaskdf law in LA is stuck. oh, iuoi020990, it's crazy ";
$ Strlen = mb_strlen ($ str, "gb2312 ");
// Randomly generate the insert location, and determine the insert frequency based on $ times
For ($ I = 0; $ I <$ times; $ I ++)
{
$ Arr [] = mt_rand (0, $ strlen );
}
$ Arr = array_unique ($ arr); // filter repeated array elements
Sort ($ arr); // sorts the array again, from small to large by value
// Start replacement
$ I = 0;
$ Str_new = "";
Foreach ($ arr as $ v)
{
$ Str_new. = mb_substr ($ str, $ I, $ v-$ I, "gb2312"). $ keyword;
$ I = $ v;
}
$ Str_new. = mb_substr ($ str, $ I, $ strlen-$ I, "gb2312 ");
Echo ($ str_new );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.