Jump to content

Make Pre-defined Functions Bold


The Little Guy

Recommended Posts

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)

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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);.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 /.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.