The Little Guy Posted April 7, 2008 Share Posted April 7, 2008 I would like to replace any function that is found in a string with a bold bold version word. I have an array with all the functions, so how can I do this? I do know I have to convert all the words to an array, then check if the word is in the array or not, after that, I need to bold any words that are functions... How can I do this? Example: random(100) -> random(100) abs(9.5) -> abs(9.5) Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 7, 2008 Share Posted April 7, 2008 You don't need regexp at all for that. If you have all the functions in an array, just set up a matching replacement array like this and run str_replace on the whole string: <?php $functions = array('abs', 'random'); $replace = array(); foreach ($functions as $f) { $replace[] = '<b>' . $f . '</b>'; } $new_string = str_replace($functions, $replace, $old_string); echo $new_string; ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2008 Author Share Posted April 7, 2008 but then that replaces words like abs3 which is a variable. I don't want that. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 7, 2008 Share Posted April 7, 2008 You don't need regexp at all for that. A regexp may be preferred depending on the context; for example, if there's a function called random_number, you're going to get random_number. To get around this, you would use word boundaries: preg_replace('/\b(' . preg_quote($word) . ')\b/i', '<b>$1</b>', $string);. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2008 Author Share Posted April 7, 2008 like this? <?php function formatCode($string){ $find = array( "~\/\/(.*)\n~" ); $replace = array( '<span style="color:darkorange">//$1</span>', ); $format = preg_replace($find,$replace,$string); //$foundFunctions = preg_match('~\s+(.*)\(~',$string,$matches); //print_r($matches); $filename = "../functions list.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $knowArr = explode("\n",$contents); $words = explode(" ",$string); $replace = array(); foreach($words as $word){ //$replace[] = '<strong>' . $word . '</strong>'; $format = preg_replace('~\b(' . preg_quote($word) . ')\b~i','<strong>$1</strong>', $string); } //$format = str_replace($words, $replace, $format); return $format; } ?> if so... Its not working View: http://gmm.phpsnips.com/functions.php?function=abs Under Examples any better suggestions? Quote Link to comment Share on other sites More sharing options...
effigy Posted April 7, 2008 Share Posted April 7, 2008 You're making the replace on $string every time, and not the updated $format. Are there any \r's in the word file? Do $string and $word contain what you expect? Also, you need to tell preg_quote which delimiter you're using if it's not the default /. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2008 Author Share Posted April 7, 2008 I don't know, I can not figure out how to do this. All that happens is it prints out the wrong stuff: [<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>ex<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>=<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>1<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>]<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>random<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>(<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>10<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>) <<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>span<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>style<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>="<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>color<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>:<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>darkorange<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>">// <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>Returns<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>7<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>span<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>random<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>(<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>10<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>) <<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>span<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>style<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>="<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>color<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>:<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>darkorange<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>">// <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>Returns<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>9<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>, <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>highest<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>value<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>span<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>random<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>(<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>55<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>) // <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>Returns<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>0<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>, <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>minimum<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>> <<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>value<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>[/<<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>><strong>strong>ex<strong>strong><<strong>strong>strong<strong>strong>>strong>strong>strong<strong>strong>>] When I use this: <?php function formatCode($string){ $find = array( "~\/\/(.*)\n~" ); $replace = array( '<span style="color:darkorange">//$1</span>', ); $format = preg_replace($find,$replace,$string); //$foundFunctions = preg_match('~\s+(.*)\(~',$string,$matches); //print_r($matches); $filename = "../functions list.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $knowArr = explode("\n",$contents); //$words = explode(" ",$string); $words = preg_split("~(\s|\(|\])~",$string); //print_r($words); $replace = array(); $find = array(); foreach($words as $word){ if(in_array($word,$knowArr)){ $find[] = '~\b' . $word . '\b~'; $replace[] = '<strong>$1</strong>'; } } echo '<br><br>'; //if(in_array($word,$knowArr)){ //$format = preg_replace('~\b(' . preg_quote($word) . ')\b~','<strong>$1</strong>', $format); $format = preg_replace($find,$replace, $format); //} //$format = str_replace($words, $replace, $format); return $format; } ?> The ideal final result should would look great if it looked like this: random(10) // Returns 7 random(10) // Returns 9, highest value random(55) // Returns 0, minimum value Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 7, 2008 Author Share Posted April 7, 2008 I would actually like to make the function a link instead of bold, can that be done? (I know it can but I don't know how) 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.