dreamwest Posted February 12, 2009 Share Posted February 12, 2009 How can i strip common words from a string?? Original string: She walks down the street at night Strip common words "the" and "at" : She walks down street night Im thinking an array, but ive never done arrays before... Quote Link to comment Share on other sites More sharing options...
Prismatic Posted February 12, 2009 Share Posted February 12, 2009 <?php $wordlist = array("the", "at"); $string = "She walks down the street at night"; foreach($wordlist as $word) $string = str_replace($word, "", $string); print $string; ?> Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 12, 2009 Share Posted February 12, 2009 Arras are really simple once you get into them, and you cannot code without them . <?php $disallowed = array( "at", "the", "a", "or", "and" ); // and about 1 hundred other words lol. ?> then you may need a more advanced regex pattern, to use "preg_replace". Ask in the regex forum for help with that, something like: "/\A[ ]{0,1}".$current_word."[ ]{0,1}$/i" - this should work, but there might be more effective methods. My Example: <?php $disallowed = array( "at", "the", "a", "or", "and" ); // and about 1 hundred other words lol. //set string $string = $_POST['search']; // $_POST from form data. //loop foreach($disallowed As $current_word){ $string = preg_replace("/\A[ ]{0,1}".$current_word."[ ]{0,1}$/i"," ",$string); } echo($string); ?> The aim is to not leave half a word, eg: if ou searched for 'sweat', and ou filtered 'at', ou would be left with 'swe'. So you need a regex pattern that could handle that. or use a different method - like splitting the string into an array itself and remove specific values. - uniflare Quote Link to comment Share on other sites More sharing options...
dreamwest Posted February 12, 2009 Author Share Posted February 12, 2009 None of these work... My string is "She walks down the street at night" and i used all the examples but the string stays the same $disallowed = array( "at", "the", "a", "or", "on", "On", "and" ); // and about 1 hundred other words lol. //set string $string = "She walks down the street at night"; //loop foreach($disallowed As $current_word){ $string = preg_replace("/\A[ ]{0,1}".$current_word."[ ]{0,1}$/i"," ",$string); } echo($string); Quote Link to comment Share on other sites More sharing options...
Prismatic Posted February 12, 2009 Share Posted February 12, 2009 None of these work... My string is "She walks down the street at night" and i used all the examples but the string stays the same $disallowed = array( "at", "the", "a", "or", "on", "On", "and" ); // and about 1 hundred other words lol. //set string $string = "She walks down the street at night"; //loop foreach($disallowed As $current_word){ $string = preg_replace("/\A[ ]{0,1}".$current_word."[ ]{0,1}$/i"," ",$string); } echo($string); Use this <?php $wordlist = array("the", "at"); $string = "She walks down the street at night atleast"; foreach($wordlist as $word) $string = preg_replace("/\s". $word ."\s/", " ", $string); print $string; ?> Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted February 12, 2009 Share Posted February 12, 2009 \s or \b for word boundary? Quote Link to comment Share on other sites More sharing options...
Zhadus Posted February 12, 2009 Share Posted February 12, 2009 \b would be word boundary, \s would be a whitespace, which works just fine in a sentence without symbols. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted February 12, 2009 Author Share Posted February 12, 2009 <?php $wordlist = array("the", "at"); $string = "She walks down the street at night atleast"; foreach($wordlist as $word) $string = preg_replace("/\s". $word ."\s/", " ", $string); print $string; ?> This works. Thanks Quote Link to comment 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.