play_ Posted January 8, 2007 Share Posted January 8, 2007 What's the best way for doing this?I have this array:[code]$keywords = array ( "and", "del", "from", "not", "while", "as", "elif", "global", "or", "with", "assert", "else", "if", "pass", "yield", "break", "except", "import", "print", "class", "exec", "in", "raise", "continue", "finally", "is", "return", "def", "for", "lambda", "try" );[/code]I would like to search for these words in a string (and hilite them).I am thinking, i could count the array, and use a for loop and during each loop, search and replace an element with a hilited element.Is there a better way? Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/ Share on other sites More sharing options...
Clarkey_Boy Posted January 8, 2007 Share Posted January 8, 2007 I would personally use the following array:$keywords = array ( 0 => "and", "del", "from", "not", "while", "as", "elif", "global", "or", "with", "assert", "else", "if", "pass", "yield", "break", "except", "import", "print", "class", "exec", "in", "raise", "continue", "finally", "is", "return", "def", "for", "lambda", "try" );(it would then count up from 0)Use the following code, where $searchstring is the string you want to search through:for each($keywords As $no => $keyword){str_replace($keyword, $keywordreplacement, $searchstring);}RC Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/#findComment-155511 Share on other sites More sharing options...
effigy Posted January 8, 2007 Share Posted January 8, 2007 Try[tt] preg_replace_callback [/tt]with[tt] /([a-z]+)/[/tt], where the callback checks to see if the match is in the array. Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/#findComment-155514 Share on other sites More sharing options...
fert Posted January 8, 2007 Share Posted January 8, 2007 http://us2.php.net/manual/en/function.in-array.php Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/#findComment-155518 Share on other sites More sharing options...
play_ Posted January 8, 2007 Author Share Posted January 8, 2007 Thanks guys.If anyone know which method is faster, spill it out ;) Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/#findComment-155522 Share on other sites More sharing options...
play_ Posted January 8, 2007 Author Share Posted January 8, 2007 [quote author=fert link=topic=121454.msg499474#msg499474 date=1168227572]http://us2.php.net/manual/en/function.in-array.php[/quote]I need to do the opposite of that. unless i can use an array inside in_array() Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/#findComment-155525 Share on other sites More sharing options...
Clarkey_Boy Posted January 8, 2007 Share Posted January 8, 2007 To me, my way is far easier to code and understand... but then I have never tried the preg_replace way - dont even know what it does.RC Link to comment https://forums.phpfreaks.com/topic/33287-solved-searching-array-elements-in-a-string/#findComment-155531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.