Is there no way to leave the match when using regex and php
or There is no way
UPDATE I am trying to remove all instances of the string from another string, but I want to retain the example.
Removing $ = 'Test'; $ String = 'This test string is a test string to remove the word test;'; Oput string will be: This is a string string to delete the test > test < / P>
Easy PHP Method:
& lt ;; Php $ pattern = "/ a / i"; $ Text = "banan a"; Preg_match ($ patterns, $ text, $ match, PREG_OFFSET_CAPTURE); Preg_match ($ pattern, $ text, $ matches, 0, $ matches [0] [1]); Match $ [0]; ? & Gt;
will give you "AN".
UPDATE : Did not know that it was a replacement. Try it out:
& lt ;? Php $ toremove = 'test'; $ String = 'This test string is a test string to remove the word test;'; Preg_match ("/ $ delete /", $ string, $ matches, PREG_OFFSET_CAPTURE); $ NewString = preg_replace ("/ $ delete /", "", $ string); $ NewString = substr_replace ($ new string, $ matches [0] [0], $ matches [0] [1], 0); Echo $ newString; ? & Gt;
Find the first match and remember where it was, then remove everything, then put whatever was in the first place.
Comments
Post a Comment