I want to change all the words of 1 or 2 characters by stars. I currently use this rule: / P>
$ validTxt = preg_replace ("/ \\ b \\ w {1,2} \\ b /", '*', $ validTxt); But when words are 2 letters, then it is replaced with only 1 star, not 2. I want a fast way to do this, display is a priority, even if it needs 100 lines.
To remove the correct time, 'e' modifiers which you can use in the replacement string Allows executable code to be kept. (Note that this 'e' modifier can only be used with preg_replace () ). This is the tested task step: function terms_stost ($ text) {$ replaced = 'str_repeat ("*", strlen ("$ 0"));'; Return preg_replace ('/ \ b \ w {1,2} \ b / e', $ replaced, $ text); } Note that this method will not be as fast as the callback solution of Nickicon, but I thought I would answer this ring in any way (' Not easy to know about.)
Update: Benchmark, O my! I was curious about which method would be the fastest, and so much, so I measured the speed of different methods using a simple Benchmark function I'm floating around I used to implement each one correctly Taken in three ways and created the function. (Note that I had to modify Nick's solution because my PHP 5.2.14 was not liked by placing the syntax directly in the preg_replace_callback () parameter.) The functions given here are measured : // Ridderunner's "A_modifier" method: function word_stost ___________ motor ($ valid text) {return preg_replace ('/ \ b \ w {1,2} \ b / e', 'str_repeat ("*", Strlen ("$ 0"); ', $ valid text); } // "callback" method of legal (modified): function word_stost_stress_colorback ($ valid text) {return preg_replace_callback ('~ \ b \ w {1,2} \ b ~', '_words_to_stars_callback', $ validTxt); } Function _words_to_stars_callback ($ match) {return str_repeat ('*', strlen ($ matches [0])); } // EVA 611, "array" method of matte and match: function words_sth_star_re ($ valid text) {return preg_replace (array ('/ \ b \ w {1} \ b /', '/ \ b \ w {2 } \ B / '), array (' * ',' ** '), $ valid text); } I took text from this very webpage as test data and then measured how much time each function had to do (on my AMD64 3700+ Win32 XP box). Here are the results of the benchmark test: (Drumolroll, please!)
e_modifier () NRPS: 133 Time: 1.050 S Function Time: 0.007893 seconds Callback () NREPS: 213 Time: 1.034 S Time of Function : 0.004856 seconds Array () NRPS: 578 Time: 1.016 Function Time: 0.001758 sec As I suspect, the modification method was slow; 'e' Callback method II and EVA 611, array of matte and navigable methods won by landslide!
Comments
Post a Comment