Showing posts with label 3-Strings. Show all posts
Showing posts with label 3-Strings. Show all posts

String Capitalization Functions: strtoupper-strtolower-ucwords.

In php you can easily manipulate the capitalization of your PHP strings, there are ready to use functions that help you to convert yout text to upper case, lower case, or just the first letter of every word to upper case.

<?php

$originalString = "Testing string Capitalization AbCDe";

$upperCase = strtoupper($originalString); // Result: TESTING STRING CAPITALIZATION ABCDE
$lowerCase = strtolower($originalString); // Result: testing string capitalization abcde
$ucTitleString = ucwords($originalString); // Result: Testing String Capitalization Abcde

?>


This does explain everything about how to manipulate capitalization. In fact strtoupper returns the string in upper case, strtolower converts to lower case, and ucwords capitalize the first letter of each word, without making any changes to other characters in the word. (ie tEsT becomes TEsT with ucwords).

If you have any question, I'm ready to help you.

PHP Random code Generator

This is a very simple-to-use php function that help you generate random codes in very easy way, you will need this function to generate random passwords, confirmation links,...

So, this is it:
<?php
function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 9) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
?>


That's it!
There are many options you can play with, for example you can choose the characters to generate code from: $chars ="1234567890" will generate numeric code; $chars = "abcdefghijkmnopqrstuvwxyz023456789"; will generate alphanumeric codes. In fact the code will be generated randomly from the list of characters in $chars.

You can also change the lenght of the generated code by changing the value in "while ($i <= 9)", 9 here means you will get a code with 10 characters, as this counter (while) starts from 0 to 9. If you do "while ($i <= 7)" you will get 8 characters code.

This is the easiest and the best way to generate random code from php. I will always provide the best PHP scrips :)

If you like my blog, please give a link to us from your website, I will appreciate that :)

ob_start - save php output to a string - The PHP output buffering

I want to share this great php utility: the output buffering, it is sometimes very usefull. So what it does ?
The PHP output buffering will save all the server outputs ( html and php prints) to a string variable.

So to start buffering, use ob_start(); this will keep saved any output.
Then you use $variable = ob_get_clean(); to stop buffering, and copy the buffer content to the variable.

Here are few samples of the use of ob_start() and ob_get_clean()


<?php ob_start(); //Turn on output buffering ?>
Hello world, <a href="http://www.blogger.com/myotherpage.php">link</a>
<div class="style">Content</div>
<?php $var = ob_get_clean(); //copy current buffer contents into $message variable and delete current output buffer ?>


Now the content of the variable $var should be:

Hello word, <a href="http://www.blogger.com/myotherpage.php">link</a>
<div class="style">Content</div>


Another example (with php outputs buffered)

<?php ob_start(); ?>
Hello world, <?php echo "My Website"; ?>
Thank you.
<?php $var = ob_get_clean(); ?>


And this will be the content of $var:
Hello world, My Website
Thank you.

That's all !

PHP eval - Execute php script in a string

Do you want to be able to run a php script in a string ?
This could be very helpful, for example if you want to use a module or a template, you may have a php script in a string, just like that:
Hello echo $username; ?> . You are wellcome !
What if you have this in a text file ? or in a database ? What should you do ?
If you loads that text in a string, and echo it to the html page, it will not be executed by php.
<?php
$string = "
Hello ?> . You are wellcome !";
echo $string;
?>
Display:
Hello ?> . You are wellcome !
So how to make php execute it ?

If you have only a php script in a string, you just use eval() to execute:
<?php
$string = "
echo $username; ";
eval ( $string ) ;
?>
This is the same thing than:
<?php
echo $username;
?>
If you have a string that is a html with some php tags, like the first example, do the following:
<?php

$username = "SMITH" ;
$string = "Hello . You are wellcome !";

// This will search for the php script in the string:
$pos = strpos( $tmp ,"" ) ;
$pos2 = strpos ( $tmp , "?>" , $pos ) ;
$scr = substr($tmp,$pos + 5 , $pos2 - $pos -3 ) ;

//This will execute the script.
echo substr($tmp,0,$pos);
eval( " $scr " );
echo substr($tmp,$pos2+2);


?>

Display:
Hello SMITH . You are wellcome !