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.