thechad Posted March 30, 2009 Share Posted March 30, 2009 I've tried to handle this a few ways, and none of them seem to work. Warning: preg_replace() [function.preg-replace]: Empty regular expression in /home/content/index.php on line 32 $_SESSION['keywords'] = get_search_phrase($_SERVER["HTTP_REFERER"]); $key = $_SESSION['keywords']; if (empty($key)) { $key = "nokeywords"; } $words = preg_replace(" ", "0", $key); //replace any spaces with "0". Now I've also tried it this way.... $_SESSION['keywords'] = get_search_phrase($_SERVER["HTTP_REFERER"]); $key = $_SESSION['keywords']; if ($key == "") { $key = "nokeywords"; } elseif ($key == NULL) { $key = "nullkeywords"; } $words = preg_replace(" ", "0", $key); //replace any spaces with "0". Neither one worked... Maybe there's an easier way to do it? What I'm actually trying to accomplish is to strip out ANY character in the variable that is not alpha/numeric. I only want numbers and letters in the strings. I know I'm only currently going after spaces, but maybe there's an easier function to accomplish this that I'm unaware of. Thanks for your input in advance. Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/ Share on other sites More sharing options...
Ayon Posted March 30, 2009 Share Posted March 30, 2009 uhm.. you can't do a preg_replace like that... should use str_replace instead for what you're trying to do... preg_replace needs a pattern to work with.. Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/#findComment-796752 Share on other sites More sharing options...
thechad Posted March 30, 2009 Author Share Posted March 30, 2009 Thanks, I made that change, and I understand you're exactly right. Now is there a way I can parse through the string and remove any characters that are NOT alpha/numeric? I've using this value to pass a variable in the hyperlink, and if any characters other than letters or numbers appear, the whole string is simply ignored. Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/#findComment-796766 Share on other sites More sharing options...
Ayon Posted March 30, 2009 Share Posted March 30, 2009 i'm not that good with regex but you could try preg_replace("/[a-zA-Z0-9-]+/","",$var) or something... Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/#findComment-796785 Share on other sites More sharing options...
sasa Posted March 30, 2009 Share Posted March 30, 2009 preg_replace("/[^a-zA-Z0-9-]+/","",$var); Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/#findComment-796788 Share on other sites More sharing options...
thechad Posted March 30, 2009 Author Share Posted March 30, 2009 thanks, that should do it.... Although I replaced with a space, and then the next line I replace spaces with a 0.... I want to know where the other characters were, I just want to replace them with a 0 Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/#findComment-796794 Share on other sites More sharing options...
Ayon Posted March 30, 2009 Share Posted March 30, 2009 preg_replace("/[^a-zA-Z0-9-]+/","",$var); oh i was so close Quote Link to comment https://forums.phpfreaks.com/topic/151729-solved-preg_replace-empty/#findComment-796801 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.