honeyphp Posted May 18, 2008 Share Posted May 18, 2008 I used preg_replace 48 times in my program and it works slowly. I want to replace preg_replace with str_replace.but it doesn't work ,because str_replace can not accept regular expression just string or array. preg_replace: $name= "12e%1223%?!"; $name1= preg_replace('/[^a-z0-9\-_]+/i', '', $name); echo $name1; str_replace: $name= "12e%1223%?!"; echo "<br>"; $name3= str_replace('/[^a-z0-9\-_]+/i', "", $name); echo $name3; my questions are: 1.is there possibility to replace preg_replace with srt_replace? 2.if not ,is there another function that i can use? 3.is there a function that i can conver characters to string? Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/ Share on other sites More sharing options...
Orio Posted May 18, 2008 Share Posted May 18, 2008 My server is down so I can't test it or find out if it's faster the using preg_replace(). But try it: <?php function only_alpha_num($str) { $valid_chars = range(65,90) + range(97,122) + range(48,57); //A-Z + a-z + 0-9 $len = strlen($str); for($i=0, $new_str = ""; $i< $len; $i++) $new_str .= (in_array(ord($str[$i]), $valid_chars)) ? $str[$i] : ''; return $new_str; } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/#findComment-544316 Share on other sites More sharing options...
Orio Posted May 18, 2008 Share Posted May 18, 2008 It seems like using preg_match() is faster here... Try checking the Filter library, maybe you could find something over there. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/#findComment-544338 Share on other sites More sharing options...
honeyphp Posted May 18, 2008 Author Share Posted May 18, 2008 your code seems perfect (thank you very much )but it doesn't work ,because the + between arrays doesn't work do you know how can ik combine the arrays? range(65,90)+range(97,122).... <?php function only_alpha_num($str) { $valid_chars = range(65,90) + range(97,122) + range(48,57); //A-Z + a-z + 0-9 $len = strlen($str); for($i=0, $new_str = ""; $i< $len; $i++) $new_str .= (in_array(ord($str[$i]), $valid_chars)) ? $str[$i] : ''; return $new_str; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/#findComment-544425 Share on other sites More sharing options...
cx323 Posted May 19, 2008 Share Posted May 19, 2008 http://us2.php.net/manual/en/function.array-merge.php should do what you want Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/#findComment-544649 Share on other sites More sharing options...
Orio Posted May 19, 2008 Share Posted May 19, 2008 Yep cx323 is right.. It's a place for array_merge() and not the "+" operator since only array_merge() appends the values of arrays that use numeric keys and doesn't overwrite. The line should be: $valid_chars = array_merge(range(65,90) , range(97,122) , range(48,57)); Orio. Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/#findComment-544672 Share on other sites More sharing options...
effigy Posted May 19, 2008 Share Posted May 19, 2008 I used preg_replace 48 times in my program and it works slowly. Why do you use it 48 times? Are all of the patterns the same? Quote Link to comment https://forums.phpfreaks.com/topic/106189-solved-replace-preg_replace-with-another-function-like-str_replace/#findComment-544827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.