HDFilmMaker2112 Posted June 8, 2012 Share Posted June 8, 2012 I really can't figure out how to set-up the conditionals in this situation. The function should take the the string, look through it and if it founds a matching word in the string to the words in the array list, replace it. The search should be case-insensitive, but the output of ASCII code should match the original input case on all letters. I'm not sure where to properly put the if statement or how/what I should be using to do the matching. I was figuring preg_match, but I have no idea how to use that with an array. EDIT: Maybe a foreach to iterate over the array, with a preg_match matching any full word (if that's possible); then returning a true or false if a match is found, then wrap the second foreach in an if statement checking to see if the value is true? <?php error_reporting(E_ALL); function replace_word($string){ $words = array('Test', 'Test2'); foreach($words as $word){ if($word==$string){ $ascii = ''; $index = 0; while($index < strlen($word)) { $ascii .= "&#".ord($word[$index]).";"; $index++; } echo $ascii . '<br />'; } } return $string; } $text="This is a test."; echo replace_word($text); ?> The expect output should be "This is a (ASCII code for the word "test").". Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 Alright, I came up with the following; but it's returning a blank page. <?php error_reporting(E_ALL); function replace_word($string){ $text=""; $words = array('TEst', 'Test2'); foreach($words as $word){ if(stristr($string, $word) === TRUE) { $ascii = ''; $index = 0; while($index < strlen($word)) { $ascii .= "&#".ord($word[$index]).";"; $index++; } $text=str_replace($word, $ascii, $string); } } return $text; } $text="This is a test."; echo replace_word($text); ?> Quote Link to comment Share on other sites More sharing options...
Drummin Posted June 8, 2012 Share Posted June 8, 2012 Not sure if this will help you, but this is my take on it. <?php error_reporting(E_ALL); function replace_word($string){ $words = array('Test', 'test2'); $ascii = ''; for ($i=0;$i < count($words);$i++){ $ascii .= "&#".ord($words[$i]).";"; $string=str_replace($words[$i], $ascii, $string); } return $string; } $text="This is a Test and this is test2 and this is test."; echo replace_word($text); ?> Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 I modified it a bit. It works, but not quite how I want it to. <?php error_reporting(E_ALL); function replace_word($string){ $words = array('Test', 'test2'); for ($i=0;$i < count($words);$i++){ $ascii = ''; $index = 0; while($index < strlen($words[$i])) { $ascii .= "&#".ord($words[$i][$index]).";"; $index++; } $string=str_replace($words[$i], $ascii, $string); } return $string; } $text="This is a Test and this is test2 and this is test."; echo replace_word($text); ?> It does replace "Test" and "test2" but I also want it to replace "test". It should be a case insensitive search, but a case sensitive ASCII code generation. So if "Test" and "test" are found in the string it should replace both, but they should be replaced with the ASCII that would generate "Test" and "test" respectively. It should also find TeSt, tEsT, TesT, tEST, etc. and replace with the ASCII to recreate those capitalizations. I could manually put in every single possible capitalization of the words, but that would be a nightmare to make sure I don't miss anything. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 8, 2012 Share Posted June 8, 2012 str_ireplace Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 str_ireplace I had thought the same thing, but that returns "Test" no matter what the initial capitalization. Also test2 is processed as test, so the ASCII code ends up looking like this: This is a Test and this is Test2 and this is Test. (This form software converts ampersands into & #38; so that's what those are in the php box.) Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 8, 2012 Share Posted June 8, 2012 Ah, I see what you want now. You should do a preg_match I think, to find the case insensitive matches, then parse them to replace with the case sensitive new values. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 Well I came up with this preg_match... I just have no idea where to put it, or what to run in the If statement. if(preg_match_all("/\b$words[$i]\b/i", $string)){ } That would only work of course, if it's inside the for loop. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 8, 2012 Author Share Posted June 8, 2012 Still not having any luck. I could generate the array with each capitalization manually, but I'm looking at an array with nearly 300 values in it then. Quote Link to comment Share on other sites More sharing options...
HDFilmMaker2112 Posted June 9, 2012 Author Share Posted June 9, 2012 Solved with this. function make_ascii($word) { $arr = str_split($word); foreach ( $arr as &$c ) { $c = '&#'.ord($c).';'; } return implode($arr); } function replace_word($string){ $words = array(); foreach ($words as $word) { $string = preg_replace('/\b'.preg_quote($word).'\b/ie', "make_ascii('\\0')", $string); } return $string; } 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.