Use a random number to rotate multiple URLs in the document. the random numbers at the end of each URL must be different.

Source: Internet
Author: User
Replacing multiple URLs in an article with a random number requires that the random numbers following each URL be different. There are many articles in the database. There are many URLs in this article. I want to add random numbers to the URLs, but the random numbers behind each URL are different. I wrote a replacement option, but the result is that after the replacement, the random numbers after each URL are the same for ($ I = 1; $ I & lt; = 60; $ I ++) {$ du: replace multiple URLs in the document with a random number. the random numbers after each URL must be different.
There are many articles in the database. There are many URLs in the article
I want to add a random number after the URL, but the random number after each URL is different.

I wrote a replacement option, but the result is that the random numbers after each URL are the same after the replacement.

For ($ I = 1; $ I <= 60; $ I ++ ){
$ Duoyu = revert (1,111 );
$ Res-> fn_ SQL ("update content set body = replace (body, 'com', 'com $ duoyu') where aid = '$ I '");
}

Please help

------ Solution --------------------
The above code does not guarantee that the random numbers generated in a loop are unique. if the requirements are strict, you need to modify the code:
PHP code
Function addRandNumber ($ matches) {global $ ar; $ n = rand (1,111); while (in_array ($ n, $ ar) $ n = rand (1,111 ); $ ar [] = $ n; return $ matches [1]. $ n ;}$ body = "url_com
Url_com
Url_com "; $ ar = array (); $ body = preg_replace_callback (" | (com) +? | ", 'Addrandnumber', $ body );
------ Solution --------------------
Jquery implementation. Very simple.

$ ("A"). each (function (index, obj)
{
$ (Obj). attr ("href", $ (obj). attr ("href") + "? Rand = "+ Math, random );
})
------ Solution --------------------
Discussion

I can see that csdn experts are always a few

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.