Cainiao asks for instructions on how to replace the attachment tag in the document cyclically
$ 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
Reply to discussion (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.
Thank you. the short and brilliant method is so easy to use.