Broad Network


PHP String Manipulation

PHP String with Security Considerations - Part 6

Foreword: In this part of the series I talk about the handling of strings in PHP.

By: Chrysanthus Date Published: 29 Oct 2018

Introduction

This is part 6 of my series, PHP String with Security Considerations. In this part of the series I talk about the handling of strings in PHP. You should have read the previous parts of the series before coming here, as this is the continuation.

Extension of the Alphabet Series to Other Characters
In the dictionary, words are written in alphabetical order. In PHP, a word beginning with D is considered greater than a word beginning with B. In PHP there is also the issue of case sensitivity. b is greater than B; that is, lowercase letters come after uppercase letters. In PHP ‘A’ is greater than 9. And 9 is greater than 0.

You can now imagine a PHP dictionary consisting of alphanumeric words. In this situation, the alphanumeric series order is, 01—9AB –Zab—z. The numbers come first before the uppercase letters and then the lowercase letters. What comes after in the series is considered greater than what comes before. String comparison in PHP uses this order.

The strnatcmp() Function
The syntax is:

    int strnatcmp ( string $str1 , string $str2 )

The function returns a negative number if $str1 is less than $str2; a positive number if $str1 is greater than $str2; and 0 if they are equal. Try the following code:

<?php

    echo strnatcmp ('alphabetical', 'order'), '<br>';
    echo strnatcmp ('bcde', 'bcde'), '<br>';
    echo strnatcmp ('man', 'MAN'), '<br>';
    echo '<br>';
    echo strnatcmp ('img1.png', 'img2.png'), '<br>';
    echo strnatcmp ('img2.png', 'img10.png'), '<br>';
    echo strnatcmp ('img10.png', 'img12.png'), '<br>';
    
?>

The output is,

-1
0
1

-1
-1
-1

as expected.

The case-insensitive version for the function is:

    int strnatcasecmp ( string $str1 , string $str2 )

Try the following code:

<?php

    echo strnatcasecmp ('book', 'Booking');

?>

The output is,

-1

as expected (in the dictionary, book comes before booking, independent of b or B).

Joining Strings
Use the dot operator to join two strings.

Splitting a String
The function to split a string by string is:

    array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

It returns an array of substrings, each of which is a substring of string, obtained by splitting the string on boundaries formed by the $delimiter argument.

Try the following code, where the delimiter is a single space:

<?php

    $str = 'one two three';

    $arr = explode(' ', $str);

    for ($i=0; $i<count($arr); ++$i)
        echo $arr[$i], '<br>';

?>

The output is:

    one
    two
    three

Note: the delimiter is not part of any of the result values.

Try the following code where the delimiter is comma followed by a single space:

<?php

    $str = 'one, two, three';

    $arr = explode(', ', $str);

    for ($i=0; $i<count($arr); ++$i)
        echo $arr[$i], '<br>';

?>

The output is:

    one
    two
    three

Try the following code which uses the $limit argument:

<?php

    $str = 'one, two, three, four, five';

    $arr = explode(', ', $str, 3);

    for ($i=0; $i<count($arr); ++$i)
        echo $arr[$i], '<br>';

?>

The output is:

one
two
three, four, five

Note the effect of the $limit argument.

That is it for this part of the series. We take a break here and continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments