How does preg_replace replace Replace the same content in sequence?

Source: Internet
Author: User
$ Row [& #039; goods_desc & #039;] contains many alt & quot; I need to set the first alt & quot; replace & quot; with the replaced content 11111 & quot; the second alt & quot; with the replaced content 22222 & quot; replace the third alt & quot; with the content after & quot; replace 33333 & quot; and so on until all alt & quot ;... $ row ['goods _ desc'] contains many alt = ""
I need to replace the first alt = "" with "with the replaced content 11111"
The second alt = "" Replace with 22222"
The third alt = "" Replace with "Replace with" content after replacement 33333"
And so on until all alt = "" In the content is replaced.
Could you write the code for this requirement? The original code is as follows: The result of the original code implementation is to replace all alt = "" With $ row ['goods _ name']
This is not the expected result.
$ Row ['goods _ desc'] = preg_replace ('/alt = ([\' | \ "]) {2}/', 'alt = "'. $ row ['goods _ name']. '"', $ row ['goods _ desc']);

Reply content:

$ Row ['goods _ desc'] contains many alt = ""
I need to replace the first alt = "" with "with the replaced content 11111"
The second alt = "" Replace with 22222"
The third alt = "" Replace with "Replace with" content after replacement 33333"
And so on until all alt = "" In the content is replaced.
Could you write the code for this requirement? The original code is as follows: The result of the original code implementation is to replace all alt = "" With $ row ['goods _ name']
This is not the expected result.
$ Row ['goods _ desc'] = preg_replace ('/alt = ([\' | \ "]) {2}/', 'alt = "'. $ row ['goods _ name']. '"', $ row ['goods _ desc']);

The first parameter of preg_replace corresponds to the second parameter in an array.


  

The above routine will output:

The slow black bear jumped over the lazy dog.

Preg_replace_callback

Use preg_replace_callback

Use this method if you want it.

$ String = "your text"; $ array = $ your_row; $ count = 1; function your_call_back ($ matches) {global $ count; $ count = intval ($ count ++); // the value of $ count here is the number of calls you have made. If you need to consider whether the array is out of bounds, you can // isset $ array [$ count] to obtain whether the array is out of bounds. return "The replaced content ". $ array [$ count]. "_". $ count;} echo preg_replace_callback ('/alt = ""/', "your_call_back", $ string );


  

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.