How to replace the attachment tag in the document cyclically

Source: Internet
Author: User
Cainiao asks for instructions on how to repeatedly replace the attachment tag in the article. This post was last edited by qq33184 from 2015-09-0600: 32: 33 $ article [content] & nbsp; str_replace ([attach_40], & nbsp; $ this-& gt; get_attach_url (40)

This post was last edited by qq33184 on 00:32:33


$ Article ['content'] = str_replace ('[attach_40]', $ this-> get_attach_url ('40'), $ article ['content']);
$ Article ['content'] = str_replace ('[attach_41]', $ this-> get_attach_url ('41'), $ article ['content']);
$ Article ['content'] = str_replace ('[attach_42]', $ this-> get_attach_url ('42'), $ article ['content']);

Replacement rule
$ Article ['content'] = preg_replace ('/\ [attach _ (\ d +) \]/is', $ this-> get_attach_url ('\ 1 '), $ article ['content']);


Note:
$ Article ['content'] content
[Attach_42] in the attachment tag, the number indicates the ID of the attachment in the data table.
The get_attach_url () method obtains the complete attachment path, for example, http: // 127.0.0.10/attach/2015-09-05/1441449289 kWkS.jpg.

Cainiao asks for instructions on how to replace the attachment tag in the document cyclically
------ Solution ----------------------
Php5.5 or lower
$ Article ['content'] = preg_replace ('/\ [attach _ (\ d +) \]/ise', '$ this-> get_attach_url ("\ 1 ") ', $ article ['content']);

All versions are supported.
$ Article ['content'] = preg_replace_callback ('/\ [attach _ (\ d +) \]/is', array ($ this, 'get _ attach_url '), $ article ['content']);
However, you need to modify the get_attach_url method (change the array parameter to a single value parameter)

Php5.4 or above
$ Article ['content'] = preg_replace ('/\ [attach _ (\ d +) \]/is', function ($ m) {return $ this-> get_attach_url ($ m [1] ;}, $ article ['content']);
Although php5.3 supports closures, $ this cannot be used in closures.


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.