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.