Calculate a regular rotation in PHP

Source: Internet
Author: User
To replace a regular expression in PHP, you need to replace the characters cjy/dongnanya/flb-mnl/xianlu & nbsp; replace it with cjy/dongnanya-flb-mnl-xianlu, that is, replace the original character with "-" instead of the first "/", and replace all others with "-" to replace a PHP regular expression.
Replace the character cjy/dongnanya/flb-mnl/xianlu with cjy/dongnanya-flb-mnl-xianlu.

That is to say, do not replace the original character except the first "/", and replace all others with "-" php
------ Solution --------------------
$ S = 'cjy/dongnanya/flb-mnl/xianlu ';

// Method 1
$ T = preg_replace ('#/#', '-', $ s );
$ T = preg_replace ('#-#', '/', $ t, 1 );
Echo $ t;

// Method 2
$ N = 0;
Echo preg_replace_callback ('#/#', 'foo', $ s );

Function foo ($ r ){
Global $ n;
If ($ n ++) return '-';
Return $ r [0];
}


------ Solution --------------------
Reference:
Reference: I still like the one that can be replaced after writing a line. how can this problem be solved?

PHP code? 1234
Thank you. it's the simplest. can you explain it? thank you ......


(? <= /)(.*?) /

(? <= Content 1) content 2: content 2 is matched, and content 2 is prefixed with content 1. to put it bluntly, content 2 is matched ", however, if no content 1 exists, the matching fails,
(.*?) /: Match as few characters as possible until "/" is matched.

The entire meaning is to match as few characters as possible. this character ends with a slash (/). at the same time, it must be preceded by a slash (/) to match successfully, then replace it with the character itself, but replace the ending "/" with "-".

I do not know whether to understand.
------ Solution --------------------
Reference:
Preg_replace ("#(? <= /)(.*?) /# ", '\ 1-', $ str );

Sorry, I still don't understand why the character page replaced with memory 1 is not in memory 2.

The next time you reply, remember to reference it.
(? = )(? <= )(?!) (? ) And (? :), These will not be recorded in the capture memory.
Brackets are used in many ways ,(? #) If I remember correctly, it is a comment.
It is recommended that you have time to dive into regular expressions. there are not many knowledge points and you will be able to master them in one day. The rest is that you have nothing to do with it.

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.