Preg_replace how to replace srcn1images with srcimages preg_replace how to replace src = "/n1/images with src =" images
Src = "/n1/images may contain other characters, such as m2 or py.
Do not understand regular expression Replacement. please advise.
Reply to discussion (solution)
$s =<<< TXTTXT;echo preg_replace('#(?<=src=").*/([^"]+)#', '$1', $s);
$s =<<< TXTTXT;echo preg_replace('#(?<=src=").*/([^"]+)#', '$1', $s);
Thanks to the moderator. what should I do?
Replace it with something similar,
Your requirements are not clear, and there are conflicts. whether the file itself is retained or the file name and its parent directory are retained.
Your requirements are not clear, and there are conflicts. whether the file itself is retained or the file name and its parent directory are retained.
In fact, it is replaced
Delete the directory before images
$ Str = "src = '/n1sss/images'";
$ Zz = "/src = '\/.*\/(.*?) '/";
Echo preg_replace ($ zz, '$ 1', $ str );
$ Str = "src = '/n1sss/images'";
$ Zz = "/src = '\/.*\/(.*?) '/";
Echo preg_replace ($ zz, '$ 1', $ str );
This $ zz = "/src = '\/.*\/(.*?) '/"; An error occurs in PHP.
Thank you for your answers. maybe my question is not very clear,
How to delete the directory before images using preg_replace
Change or
Please help me again
$ S = <TXTTXT; $ n = 0; // only the file name echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 1; // keep echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 2; // keep the second-level directory echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s );
$ S = <TXTTXT; $ n = 0; // only the file name echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 1; // keep echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 2; // keep the second-level directory echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s );
$ S = <TXTTXT; $ n = 0; // only the file name echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 1; // keep echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 2; // keep the second-level directory echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s );
$ S = <TXTTXT; $ n = 0; // only the file name echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 1; // keep echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s); $ n = 2; // keep the second-level directory echo preg_replace ("#(? <= Src = ['\ "]). */(? :. */) {$ N} [^ '\ "] +) #",' $ 1', $ s );
Thanks for the answer from the moderator. I have a folder named images, and I want to keep the images folder and the following Directory. the number of levels of directories after images is unknown. Ask the moderator for help.
$ Str = "src = '/n1sss/images'";
$ Zz = "/src = '\/.*\/(.*?) '/";
Echo preg_replace ($ zz, '$ 1', $ str );
I have run it all, but it's okay.