Skip to content

This is an area to use for joint revision for the zend php exam.

Info about the exam including some ways to get practice questions can be found here.

Testing the typesafe-functions repo

The typesafe-functions repo acts as a wrapper around some of the PHP internal functions.

Testing this will allow us to learn the internal functions.

Getting the repo

Clone the scratch container 'cluster1_clone edmondscommerce-scratch'

In a sensible location, clone the typesafe-functions repo git clone git@github.com:edmondscommerce/typesafe-functions.git

It is also useful to clone the testing example to look how we should be writing tests git clone git@bitbucket.org:edmondscommerce/testing-example.git

PHP String methods

Note

The letters in the name have meaning

pos str r i
position string reverse case insensitive

Coding challenge

Heavily mangle some text with code using only str and substr functions.
Example paragraph:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into 
electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset 
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker 
including versions of Lorem Ipsum.

Functions to be used:

Catalin 3v4l url

<?php

$a = "Lorem IPSUM dolor sit amet, consectetur ipsum adipiscing elit.";

$dolor =  substr($a, 12, 5);
echo "substr from position 15, 5 characters => ".$dolor."\n";

$elit = substr($a, -5, 4);
echo "substr from position -5, 4 characters => ".$elit."\n";

// case sensitive
$dolor_position = strpos($a, "dolor");
echo "strpos of dolor (case sensitive) => ".$dolor_position."\n";

// case insensitive
$dolor_position_i = stripos($a, "dOLor");
echo "stripos of dOLlor (case insensitive) => ".$dolor_position_i."\n";

// case sensitive
$new_a = str_replace("ipsum", "hello", $a);
echo "str_replace ipsum (case sensitive) with hello => ".$new_a."\n";

// case insensitive
$new_a = str_ireplace("ipsum", "hello", $a);
echo "str_replace ipsum (case insensitive) with hello => ".$new_a."\n";

$len = strlen($a);
echo "strlen (length) of a => ".$len."\n";

Sam 3v4l url

<?php

$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into 
electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset 
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker 
including versions of Lorem Ipsum.";

$iLoc = strpos($string, 'I');
$mLoc = strpos($string, 'm', $iLoc);
$ipsum = substr($string,$iLoc, $mLoc-$iLoc+1);

// Replace all Ipsums
$string = str_ireplace(
    $ipsum,
    'String',
    $string);

// Loop through string like an array, using the index to replace 's' values
for($i = 0; $i < strlen($string); $i++){
    if($string[$i] === 's' || $string[$i] === 'S' ){
        $string[$i-1] = "\\";
        $string[$i] = '$';
    }
}

$string = str_replace(
(stristr($string, '\\', true)),
'P_H+?P',
$string);


var_dump($string);

Dale 3v4l url

Create type-safe versions, also create tests for them:

They use formatted strings. See full documentation on sprintf

<?php

$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into 
electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset 
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker 
including versions of Lorem Ipsum.";

$iLoc = strpos($string, 'I');
$mLoc = strpos($string, 'm', $iLoc);
$ipsum = substr($string,$iLoc, $mLoc-$iLoc+1);

// Replace all Ipsums
$string = str_ireplace(
    $ipsum,
    'String',
    $string);

// Loop through string like an array, using the index to replace 's' values
for($i = 0; $i < strlen($string); $i++){
    if($string[$i] === 's' || $string[$i] === 'S' ){
        $string[$i-1] = "\\";
        $string[$i] = '$';
    }
}

$string = str_replace(
(stristr($string, '\\', true)),
'P_H+?P',
$string);


var_dump($string);

Printf-3v4l

sprintf

sprintf will format a string and return it.

Example

printf - Printf 3v4l example url

<?php

$typeHandlers = [
    's',
    'd','u', 'c', 'o', 'x', 'X', 'b',
    'g', 'G', 'e', 'E', 'f', 'F', '+d',
    ];
$testNum = 22.12314123124;
foreach($typeHandlers as $type){
    printf("Type $type :  %".$type."\n", $testNum);
}


$testArr = [
    +1231241.12312312312,
    -1231241241.22231,
    0.00000000000000000000001,
    'stringy',
    ];

foreach($typeHandlers as $t){
    printf("Type $t :  %".$t." \n %".$t." \n %".$t." \n %".$t." \n",
    $testArr[0],$testArr[1], $testArr[2], $testArr[3]);
}

sscanf - Sscanf 3v4l example url

<?php

$str = "Hello 123.02 ??? 04";
$result = sscanf($str, "%s %f %s %d");
print_r($result);