NLT Posted July 3, 2012 Share Posted July 3, 2012 OK. I have a wordlist and a few words contain a hyphen (-). I'm currently using an array_search to search a string with the words from the wordlist, but I think a regular expression would be better. How can I search the string with words from the wordlist the easiest and fastest with preg_match Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/ Share on other sites More sharing options...
requinix Posted July 4, 2012 Share Posted July 4, 2012 and fastest would be to not use regular expressions. What's your code now? I bet it's better than the equivalent preg_match() calls. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359020 Share on other sites More sharing options...
NLT Posted July 4, 2012 Author Share Posted July 4, 2012 I've put them in an array and tried using array_search. But say I got a word in array like "Hyphen-", and I make the word that it'll search to "Hyphen-" it'll work fine. But if I use "Hyphen-Test" it won't. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359086 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2012 Share Posted July 4, 2012 Post the code you're using, and some of the data that fails when using it. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359094 Share on other sites More sharing options...
NLT Posted July 4, 2012 Author Share Posted July 4, 2012 Post the code you're using, and some of the data that fails when using it. $data = "Hyphen-"; // Would usually be $_POST data // Hyphen- returns "Worked". Hyphen-Test returns "Didn't". $blocked = array("Works", "Hyphen", "Hyphen-"); if (array_search($data, $blocked)) { echo "Worked"; } else echo "Didn't"; Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359114 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2012 Share Posted July 4, 2012 Both of these work properly: $data = "Hyphen-Test"; $blocked = array("Works", "Hyphen", "Hyphen-Test"); if ( array_search($data, $blocked) !== FALSE ) { echo "Worked"; } else { echo "Didn't"; } // *** AND *** $data = "Hyphen-"; $blocked = array("Works", "Hyphen", "Hyphen-"); if ( array_search($data, $blocked) !== FALSE ) { echo "Worked"; } else { echo "Didn't"; } The change to a comparison using !== FALSE is because array_search returns the array key (which could be zero) if the value is found, or FALSE if the value is not found. EDIT: That change ^^^ would not affect the outcome using the data you provided, however. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359115 Share on other sites More sharing options...
NLT Posted July 4, 2012 Author Share Posted July 4, 2012 Both of these work properly: $data = "Hyphen-Test"; $blocked = array("Works", "Hyphen", "Hyphen-Test"); if ( array_search($data, $blocked) !== FALSE ) { echo "Worked"; } else { echo "Didn't"; } // *** AND *** $data = "Hyphen-"; $blocked = array("Works", "Hyphen", "Hyphen-"); if ( array_search($data, $blocked) !== FALSE ) { echo "Worked"; } else { echo "Didn't"; } The change to a comparison using !== FALSE is because array_search returns the array key (which could be zero) if the value is found, or FALSE if the value is not found. EDIT: That change ^^^ would not affect the outcome using the data you provided, however. But if I add something after the "Hyphen-" it returns the opposite. EDIT: I don't think I made the post clear. I need it to search the whole word for that particular phrase. Not matching the blocked word with one word on it's own if you know what I mean Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359120 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2012 Share Posted July 4, 2012 The first block I posted has something after the hyphen, and it works as written. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359121 Share on other sites More sharing options...
NLT Posted July 4, 2012 Author Share Posted July 4, 2012 The first block I posted has something after the hyphen, and it works as written. But then if I remove the "Test" it returns the opposite. The thing is, it will be $_POST data so it could be anything. I want it to find, let's say: Hyphen- from Hyphen-Test and return it true/false. I want it to search the whole word for a blocked word and if it has it anywhere in the word return true/false. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359123 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2012 Share Posted July 4, 2012 That isn't the way you explained it to begin with. "hyphen-test" is a hyphenated word, "hyphen-" is a fragment of that word. I think this is more what you're after. $data = "Hyphen-test"; $blocked = array("Works", "Hyphen", "Hyphen-"); foreach( $blocked as $v ) { if( stripos( $data, $v) !== FALSE ) { echo "Found!"; break; } } Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359126 Share on other sites More sharing options...
NLT Posted July 4, 2012 Author Share Posted July 4, 2012 That isn't the way you explained it to begin with. "hyphen-test" is a hyphenated word, "hyphen-" is a fragment of that word. I think this is more what you're after. $data = "Hyphen-test"; $blocked = array("Works", "Hyphen", "Hyphen-"); foreach( $blocked as $v ) { if( stripos( $data, $v) !== FALSE ) { echo "Found!"; break; } } Yeah, that is what I wanted. I tried to explain it like that in another previous post (I think). Sorry about the confusion and thanks for the help. Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359127 Share on other sites More sharing options...
NLT Posted July 4, 2012 Author Share Posted July 4, 2012 That isn't the way you explained it to begin with. "hyphen-test" is a hyphenated word, "hyphen-" is a fragment of that word. I think this is more what you're after. $data = "Hyphen-test"; $blocked = array("Works", "Hyphen", "Hyphen-"); foreach( $blocked as $v ) { if( stripos( $data, $v) !== FALSE ) { echo "Found!"; break; } } That is what I wanted.. but I can't seem to get it to work in a function. This is my current code(It returns as "no" whatever I use as $input): function test($input) { $blocked = array("Works", "Hyphen", "Hyphen-"); foreach( $blocked as $v ) { if( stripos( $input, $v) !== FALSE ) { echo "Found!"; break; } else { echo "no"; break; } } } test("test"); Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359152 Share on other sites More sharing options...
Pikachu2000 Posted July 4, 2012 Share Posted July 4, 2012 Usually better to just return a value instead of echoing directly from within the function. <?php function test($input) { $blocked = array("Works", "Hyphen", "Hyphen-"); foreach( $blocked as $v ) { if( stripos( $input, $v) !== FALSE ) { return 'Found'; } } return 'Not found'; } echo test("Hyphen"); // echoes 'Found' echo '<br>'; echo test("not in the array"); // echoes 'Not found' Link to comment https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/#findComment-1359156 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.