PHP Tutorial : Multiple string replace with str_replace

Well, I already writed about str_replace php function but this tip is very usefull. To recap, the function it’s used to replace a string by using three paramaters : $stringToSearch, $stringToReplace, $intoString. I showed you how to use it in single operations but we can use it for multiple replaces in one place, by using arrays :

<?php
$searchArray = array("word1", "sound2", "etc3");
$replaceArray = array("word one", "sound two", "etc three");
$intoString = "Here is word1, as well sound2 and etc3";
//now let's replace


print str_replace($searchArray, $replaceArray, $intoString);
//it should print "Here is word one, as well sound two and etc three"

?>

I have to say this multiple replace “trick” is a very nice one!

See ya soon!

Posted in Basic PHP | Tagged , , , , , , | Leave a comment

PHP Tutorial : Strpos and Strrpos

Basic thing, strpos and strrpos are two php functions which are used to find the position of first and/or last occurrence in a string – word (usefull for example when you want to build a function to get file extension). I hear you saying enough talking, let me see the available examples :

<?php
//get first occurrence

$string = "this string";
$find = "i";

print strpos($string,$find);//should print 2


print strrpos($string,$find); //should print 8

?>
Posted in Basic PHP | Tagged , , , , | 1 Comment

PHP Tutorial : Get Protocol

I saw this problem/question on a few forums and I finded it useful to post on my blog too due to low results on google that gives you what you need. So,  how to get the protocol of a URL using php?  Have a look :

<?php
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https'?'https':'http';

print "The protocol is $protocol";
?>

Tadaaa!

Posted in Usefull PHP | Tagged , , , , , | Leave a comment

PHP Tutorial : Ctype character type checking

Happy 1st June for those under 18

Yesterday I’ve found a nice function in php called ctype. And? What it does? Well, I see it as a replacement for regular expressions. PHP Ctype function checks for character type. Let’s hear the chars type we can check with that nice function  :

  • alphanumeric characters – ctype_alnum()
  • alphabetic characters      – ctype_alpha()
  • control characters           – ctype_cntrl()
  • numeric characters         – ctype_digit()
  • lowercase characters      – ctype_lower()
  • uppercase characters      – ctype_upper()
  • printable characters        – ctype_print()
  • any printable character but not whitespace or an alphanumeric – ctype_punct()
  • any printable characters but not space – ctype_graph()
  • whitespace characters    – ctype_space()
  • hexadecimal digit characters – ctype_xdigit()

Ok, long list huh? but usefull. I will show you a simple example which of course can be applied for all other ctype() functions :

<?php
//will check if a character type is digit

$toCheck = 1010;

if(ctype_digit($toCheck)) {

print "$toCheck is a number";
}else{

print "$toCheck is NOT a number";
}
?>
Posted in Usefull PHP | Tagged , , , , , , , , , , , , , , , | Leave a comment

PHP Tutorial : Get line number

As you might know, PHP Server comes with built-in / already defined constants. Today I’ve been discovering a nice one which gets the line number in a file for you. This may be very usefull in a lot of cases, like a custom error system, debugging and not only. That CONSTANT is called simply _LINE_

<?php

print __LINE__;
?>

This will print 3, because it’s line two!

Posted in Usefull PHP | Tagged , , , , , | Leave a comment

Link to us

Do you enjoy our content? Did you learned something from out PHP Tutorials? If you feel you want to help us and other to learn php, please link to us :

1) simple text link

<a href="http://www.crivionweb.com/phpblog/">PHP Tutorials</a>

2) small icon

<a href="http://www.crivionweb.com/phpblog/"><img src="http://www.crivionweb.com/small-Icon.gif" border="0" alt="php tutorials"></a>
Posted in Link to us | Tagged | Leave a comment

PHP Tutorial : String lenght with strlen

I’ll be directly on this short one. Shame on me that I didn’t writed about this untill now : there’s an interesting php function which you might need frequently called strlen() which translates into string lenght. I’m sure you met it already into previous tutorials on this php tutorials blog but I think I didn’t made a post separately for this only so :

<?php
$stringLenght = "Count this characters";

print strlen($stringLenght);
?>
Posted in Basic PHP | Tagged , , , , , | Leave a comment

PHP Tutorial : If condition in the short way (shorthand if)

Do you know basical php if() condition right? But, my questions is : do you know the short way? If not, I will show you the diferrence,:

<?php
$a=1;
$b=3;
$c = $a+$b;
//THE SHORT WAY COMES
print $c == 3 ? "yes $c = 3" : "no $c != 3";
//THE "COMMON" WAY SAME THING


if($c ==3) {
print "yes $c = 3";
}else{
print "no $c != 3";
}
?>
Posted in Basic PHP | Tagged , , , , , | 2 Comments

PHP Tutorial : Extract function

A shame for me but I must tell you this function called extract() simply rocks : it will extract “keys” from an array and convert them into variables, so no more needed to manually declare variables for validation purposes from forms.

<form action="" method="post">
name <input type="text" name="name"><br/>
email <input type="text" name="email"><br/>
<input type="submit" name="sb" id="sb" value="go">
</form>
<?php

if(isset($_POST['sb'])) {
extract($_POST);

if(empty($name) || empty($email)) die("email and name emtpy");

print "Name is $name and email is $email now!";
}
?>
Posted in Usefull PHP | Tagged , , | 3 Comments

PHP Tutorial : Mysql Dump

Did you see this expression and doubt what it means? Mysql Dumps are files in different extensions/formats like .sql, .txt etc. which are used to store databases informations, table structures, mysql tables creations, table datas/entries and more. The mysql dumps are usually used to make a backup, transfer a website database when switching hosts for example, and many more…

Now you know what mysqldump means!

Posted in Php & MySQL | Tagged , , | Leave a comment