Some Random PHP Functions

Started on the old forum in November 2009 · view the original in the archive · old links redirect here

No replies · 956 views · started 17 years ago · 1 min read

riderz Lead · 975 posts #1

Math Functions:

function distance($x1, $y1, $x2, $y2)
{
	$x = pow($y2 - $y1, 2);
	$y = pow($x2 - $x1, 2);
	$distance = sqrt($x + $y);
	return $distance;
}
function midpoint($x1, $y1, $x2, $y2)
{
	$x = ($x1 + $x2)/2;
	$y = ($y1 + $y2)/2;
	$midpoint = '('.$x.','.$y.')';
	return $midpoint;
}
function calculate($equation)
{
	$equation = ereg_replace('([A-Z]|[a-z])', '', $equation);
	$answer = eval('return '.$equation.';');
	return $answer;
}

String Functions:

function array_stristr($text, $search)
{
    $num_matches = 0;
    for($i = 0; $i < count($search); $i++)
    {
        if(stristr($text, $search[$i]))
        {
            $num_matches++;
        }
    }
    return $num_matches;
}
function get_words($text, $offset, $length=null)
{
	$text = explode(' ', $text);
    if($length)
        $text = array_slice($text, $offset, $length - $offset + 1);
    else
        $text = array_slice($text, $offset);
    $text = implode(' ', $text);
    return $text;
}

Text Functions:

function alternating_caps($text)
{
	$text = strtolower($text);
	$x = 1;
	for($i = 0; $i < strlen($text); $i++)
	{
		if($x == 1)
		{
			$text{$i} = strtoupper($text{$i});
			$x = 2;
		}
		elseif($x == 2)
		{
			$x = 1;
		}
	}
	return $text;
}
function random_caps($text)
{
	$text = strtolower($text);
	for($i = 0; $i < strlen($text); $i++)
	{
		$x = round(rand(0, 1));
		if($x == 1)
		{
			$text{$i} = strtoupper($text{$i});
		}
		elseif($x == 2)
		{
		}
	}
	return $text;
}

Jacques jacques@gw-designs.co.za http://coding.biz.tm Come join and lets make it a place to learn all the noobies how to code


NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close