Home / PHP/MySQL Tutorials / Language Basics / Archive by category 'Php Basics Examples'

Php Basics Examples

Accessing string offsets in PHP

$str = "M";
$str{2} = "n";
$str{1} = "a";
$str = $str . "i";
print $str;

The above code will output “Mani”. Individual characters in a string can be accessed using the $str{offset} notation. You can use it to both read and write string offsets.


Retrieving a protected page with HTTP_Request

<?php
$r = new HTTP_Request('http://www.w3mentor.com/protect.php');
$r->setBasicAuth('username','password');
$r->sendRequest();
$page = $r->getResponseBody();

Retrieving a password protected page with cURL

<?php
$c = curl_init('http://www.w3mentor.com/protect.php');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_USERPWD, 'david:hax0r');
$page = curl_exec($c);
curl_close($c);
?>

Retrieving a password protected page using PHP

<?php
$url = 'http://username:password@www.w3mentor.com/protected.php';
$page = file_get_contents($url);
?>

Fetching a URL with HTTP_Request

<?php
require_once 'HTTP/Request.php';
$r = new HTTP_Request('http://www.w3mentor.com/robots.txt');
$r->sendRequest();
$page = $r->getResponseBody();
?>

PHP $0 Is Set To Whole Match, Then $1, $2 – String Function

<?
    $match = "/the (car|cat) sat on the (drive|mat)/";
    $input = "the cat sat on the mat";
    print preg_replace($match, "Matched $0, $1, and $2\n", $input);
?>

PHP Accessing Substrings: String Substr ( String String, Int Start [, Int Length] ) – String Function

<?php $theclientstext = "this is a test. this is another test"; if (strlen ($theclientstext) >= 30){
    echo substr ($theclientstext,0,29);
} else {
    echo $theclientstext;
}
?>

PHP Adding “S” To End Of Variable Name – String Function

<?php
            $food = "grapefruit";
            print "These ${food}s aren't ripe yet.";
            print "These {$food}s aren't ripe yet.";
    ?>

PHP Addslashes(): Escapes Quotes, Double Quotes, Backslashes, And Nulls With Backslashes – String Function

<?php
$escapedstring = addslashes("He said, 'I'm a dog.'");
$query = "INSERT INTO test (quote) values ('$escapedstring')";echo($query);
?>

PHP Alternative To Base_Convert() – String Function

<?php
if (!extension_loaded("gmp")) {
  dl("php_gmp.dll");
}function gmp_convert($num, $base_a, $base_b)
{
       return gmp_strval(gmp_init($num, $base_a), $base_b);
}echo "12345678987654321 in hex is: " . gmp_convert('12345678987654321', 10, 16) . "\n";
?>

PHP $: Anchors Pattern To End Of Line Or End Of String – String Function

 
    <?php
     $string = " is PHP";
     $pattern = "/PHP$/";if(preg_match($pattern, $string))
          print("Found a match!");
?>

PHP ^ And $ Are Line Anchors. – String Function

 
^     specifies the beginning of the line.$     specifies the end of the line.

PHP Negative Substr() Length Parameter – String Function

<?
$car = "1234567890";
$yr = substr($car, 2, -5);
print $yr;
?>

PHP Escape – String Function

 
\ indicates the escape character.

PHP Non-Capturing Optional Subpattern – String Function

<?php
$html = '<link rel="icon" href="http://www.example.com/icon.gif"/>
<link rel="prev" title="Previous" href="http://www.example.com/prev.xml"/>
<link rel="next" href="http://www.example.com/next.xml"/>';preg_match_all('/rel="(?:prev|next)"(?: title="[^"]+?")? href=
"([^"]*?)"/', $html, $linkMatches);print '$bothMatches is: '; var_dump($linkMatches);
?>

PHP Period (.) Character Is Used To Combine Two Separate Variables Into Single String – String Function

<?php
    $string = "Thank you for buying ";
    $newstring = $string . "my book!";
?>

PHP Positive Substr() Length Parameter – String Function

<?
$car = "1234567890";
$yr = substr($car, 0, 4);
print $yr;
?>

PHP Regular Expression For E-Mail String Checking – String Function

<?php
$email = "l@b.ca"; echo preg_match("/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+.(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/",$email); //Would return 1 (true). echo "<br />"; $bademail = "l.ca"; echo preg_match("/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+.(.[a-zA-Z0-9_-]+)+[a-zA-Z0-9_-]$/",$bademail); //Would return 0 (false). ?>

PHP Array Str_Split ( String String [, Int Split_Length] ) – String Function

<?php $anemail = "lee@b.ca"; $newarray = str_split($anemail);
?>

PHP Simple Use Of Preg_Match() – String Function

 
    <?php
     $string = "PHP is the web scripting language";
     $pattern = "/PHP/";if(preg_match($pattern, $string))
          print("Found a match!");
?>