$ 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 );