Preg_replace how to replace src = "/n1/images with src =" images

Source: Internet
Author: User
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.

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.