Exclude all characters after a character 3rd appears in the string. how can I write the code to exclude all characters after a character 3rd appears in the string? how can I write the code?
For example, the string "2 Hello 2 haha 2 right Ah haha"
Exclude all the characters after 3rd characters and 2 to get the result "2 Hello 2 haha 2"
Could you tell me the code? thank you.
Reply to discussion (solution)
(2) ([^ 2] *) \ 1 \ 2 \ 1
$ S = '2 Hello 2 haha 2 right Ah hahaha '; $ ar = explode ('2', $ s); echo implode ('2 ', array_merge (array_slice ($ ar, 0, 3), array ('')));
$ S = '2 Hello 2 haha 2 pair Ah hahaha '; echo preg_replace ('/((? :.*? 2) {3}). */',' $ 1', $ s );
2 Hello 2 haha 2
$ S = '2 Hello 2 haha 2 right Ah hahaha '; $ ar = explode ('2', $ s); echo implode ('2 ', array_merge (array_slice ($ ar, 0, 3), array ('')));
I am very grateful to this eldest brother for excluding how to write the last two codes.
$ S = '2 Hello 2 haha 2 right Ah hahaha '; $ ar = explode ('2', $ s); echo implode ('2 ', array_merge (array_slice ($ ar, 0, 3), array ('')));
I am very grateful to this eldest brother for excluding how to write the last two codes.
$ S = '2 Hello 2 haha 2 right Ah hahaha '; $ ar = explode ('2', $ s); echo implode ('2', array_slice ($ ar, 0, 3 ));