Title, there is a text
123456 ABCDEFGSDFSDAFDF ABCDFFSAFD
Target: Replace the second occurrence of ABC and do not operate for the first time
Rookie Solution
Reply to discussion (solution)
Replace why?
Are you sure that this text appears only once?
$test = ' test test test1 '; $arr [0] = '/(test)/'; $arr [1] = '/(YJQ)/'; $val [0] = ' yjq '; $val [1] = ' test '; $test 1 = preg_replace ( $arr [0], $val [0], $test), $test 1 = preg_replace ($arr [1], $val [1], $test); Echo $test. ': ' $test 1;
----------PHP Run----------
123456ABCDEFGSDFSDAFDF---DFFSAFD
PHP Code
$str = ' 123456abcdefgsdfsdafdfabcdffsafd ';
$str = Preg_replace ('/(ABC) (. *?) \\1/im ', ' $1$2---', $str);
Echo $str;
----------PHP Run---------......
Positive solution, I write the old long, just as long as this, very powerful ah, it seems to be in-depth study.
PHP Code
$str = ' 123456abcdefgsdfsdafdfabcdffsafd ';
$str = Preg_replace ('/(ABC) (. *?) \\1/im ', ' $1$2---', $str);
Echo $str;
----------PHP Run----------
123456<
This can't replace more ABC, right?
I have no environmental test for the time being. In case you can't, consider Preg_replace_callback.
Title, there is a text
123456abcdefgsdfsdafdfabcdffsafd
Target: Replace the second occurrence of ABC and do not operate for the first time
Rookie Solution
You are right, more than once, the regular is always faint, please explain
Well, I'll try Preg_replace_callback.
$str = ' 123456ABCDEFGABCEEESDFSDAFDFABCDFFSAFDABCDEFG ';
$str = Preg_replace ('/(? <=.
(?
)) Abc/iu ', '---', $str);
Echo $str, '
';
Only available with label only
The ↑↑ result is 123456ABCDEFG---eeesdfsdafdf---dffsafd---defg
Echo
It's a lot of writing, omitted here.
If it is
$str = ' 123456 There are other characters ABCDEFGABCEEESDFSDAFDFABCDFFSAFDABCDEFG ';
That would be a problem.
If it is
$str = ' 123456 There are other characters ABCDEFGABCEEESDFSDAFDFABCDFFSAFDABCDEFG ';
That would be a problem.
There are other characters in front of it, it is not possible to match ABC before and after the A-label regular, how to write
----------PHP Run----------
123456ABCDEFGSDFSDAFDF---DFFSAFD---dsfsdfd---sdfsdfsd---
Output complete (0 seconds elapsed)
$str = ' 123456abcdefgsdfssdfdabcdefgsdfsdafdfabcdffsafdabcdsfsdfdabcsdfsdfsdabc '; $str = Preg_replace ('/(?:. *?) (ABC) (?:.*?)|. *?) Abc/im ', ' $---', $str), echo $str, result 123456ABCDEFGSDFSSDFD---defgsdfsdafdf---dffsafd---dsfsdfd---sdfsdfsd---
The second one has been replaced, and, uh, I want to add a connection without adding a connection, and the addition is not processed.
function used in keyword processing
For example, the conductor is a keyword, the front two can not be added, no addition is only one connection, that is, the third conductor $str = 'Semiconductor materials Semiconductor manufacturing, conductor conductor conductors conductor
';
For example, the conductor is a key word, the front two can not be added, no addition is only added a connection, that is, the third conductor
$str = '
Semiconductor materials Semiconductor manufacturing, conductor conductor conductors conductor
';
The next time you describe your needs clearly, you don't really understand what you mean.
Conductor (?!. *?<\/a>)
A brief description of the regular:
1, I understand that as long as there is not followed by a closed tag, I think that the conductor is not within a link
2, there is no unified format, that is, no one can solve all the problems, to follow the demand to go
3, you can also meet the "conductor" to replace the contents of the "Guide---------Body", and then all the "conductor" plus link, and then finally the "guide---------Body" replaced with "conductor", also can get the purpose
Semiconductor materials Semiconductor manufacturing, conductor conductor conductors conductor'; $str = Preg_replace ('/(conductor) (?!. *?<\/a>)/imu ', ' $ ', $str); Echo $str;
Semiconductor material semiconductor manufacturing, conductor conductor conductor Semiconductor'; $str = Preg_replace ('/(conductor) ([^<]|< (?! \/a>) [^>]*>)/imu ', ' $1$2 ', $str); Echo $str;
I didn't consider that there was a link in the back of the case has been modified
You are too strong, I think is to determine whether there is a tag near, did not think as long as the judge has no label on it, thank you for the answer, not much, knot
Tangled up a long day
Semiconductor material semiconductor manufacturing, conductor conductor conductor Semiconductor'; $str = Preg_replace ('/(conductor) (?! (:? [^<]|< (?! A) [^>]*>) *<\/a>)/imu ', ' $ ', $str); Echo $str;
。。。。。。 This is Actually always want to ask everyone, preg_replace is not change the value of the original string, if I want to change, how to do it