Search
Syndication
Sponsors

Archive for the ‘Strings’ Category

Highlight a string

Wednesday, May 20th, 2009

<?php
$msg1 = “<?php echo \”test message to display\”;
highlight_string($msg1)
?>

Percentage Of Similar Text Between 2 Strings

Thursday, March 26th, 2009

This is a useful little fiunction you may not have heard of called similar_text.

This function will take 2 strings and compare then and return the percentage of the text that is the same as a float.

<?php

$text1 = “Welcome to getphp , we hope you enjoy our content”;
$text2 = “Hello this is getphp, we hope you like our content”;
similar_text($text1, $text2, $percent);
echo round($percent) . ” percent of text is the same”;
 
?>

replace tabs in a string

Tuesday, February 24th, 2009

<?php
$original = “string\tstring\tstring\tstring\t”;
//replace the tabs with a space
$replaced = str_replace(”\t”, ‘ ‘, $original);
echo “<pre>{$original}</pre>”;
echo “<pre>{$replaced}</pre>”;
?>

Check the amount of characters in a string function

Thursday, February 19th, 2009

<?php
//check the amount of characters in a string function
Function CheckNoChars($strText)
{
//check for between 6 and 12 characters
if (eregi(”^.{6,12}$” , $strText))
return true;
else
return false;
}
?>
<?php
//test the function
$str1 = “mypasswordistoolong”;
if (CheckNoChars($str1))
//if its OK display this message
echo “this has the correct number of characters”;
//if its not OK display this one instead
else
echo “incorrect number of characters”;
?>

Using trim to get rid of whitespace

Tuesday, February 17th, 2009

This shows how to use the trim function to remove white spaces from either side of a string

<?php
//trimming white space from a string
//our two strings
$string1 = ” spaces before”;
$string2 = ” spaces before and after “;
//remove the whitespace
$trimmed_string1 = trim($string1);
$trimmed_string2 = trim($string2);
//display the text
echo $trimmed_string1;
echo “<br>”;
echo $trimmed_string2;
?>