PHP Lines Statement

Calculate number of character per line

They are two function:

  • strlen()
  • mb_strlen() - This one must specify the text encoding.
$str = "Hello Worlds";
$str_len = strlen($str);
$mb_str_len = mb_strlen($str, 'utf-8');

echo "strlen: $str_len<br/>";
echo "strlen: $mb_str_len";

Delete characters from the beginning and end of the line

They are three function:

  • ltrim() - deletes all characters specified in the first line.
  • rtrim() - delete at the end.
  • trim() - delete the beginning and the end.
$str = "Hello Uncle Sames";
$ltrim = ltrim($str, "He");
$rtrim = rtrim($str, "es");
$trim = trim($str, "He");

echo "ltrim: $ltrim<br/>";
echo "rtrim: $rtrim<br/>";
echo "trim: $trim";

Delete characters in a row

Delete character in specify row. This can used str_replace function it performs in a row replacement.

$str = "Hello Worlds";
$lit = "o";
$str = str_replace($lit, "", $str);

echo $str;

Getting substring

Receives another parameter that specifies the character set of the strings.

$str = "Hello Worlds";

//mb_substr(text, start, lenght, 'utf-8');

$sub_0 = mb_substr($str, 4, 1, 'utf-8');
// Place on text row 4, and return 1 caharcter after point row.

$sub_1 = mb_substr($str, 1, 4, 'utf-8');
// Place on text row 1, and return 4 caharcter after point row.

$sub_2 = mb_substr($str, 1, -3, 'utf-8');

echo $sub_0 . "<br/>";
echo $sub_1 . "<br/>";
echo $sub_2;

Split sentence into words

String tokenization function, it takes two parameters: the string to be treated and the character to be on-line division into parts. Tokenization is the process of breaking a stream of text up into words, phrases, symbols, or other meaningful elements called tokens.

  • strtok()
$string = "Hello Uncle Sams";
$array_words = array();
$tok = strtok($string, " \t\n");

while($tok) {
    $array_words[] = $tok;
    $tok = strtok(" \t\n");
}

var_dump($array_words);

echo "<br/>";
echo $array_words[0] . "<br/>";
echo $array_words[1] . "<br/>";
echo $array_words[2] . "<br/>";

Split a string by string

Before you break a string by string must be defined, the string will be broken by what criterion. This separator may be, for example, a comma. It can also be a regular expression.

  • explode()
  • preg_split() - For regular expressions.
$string = "Hello uncle Sam, Today is a nice day.";
$array_words_1 = explode(',', $string);

var_dump($array_words_1);
echo '<br/>';
echo $array_words_1[0] . '<br/>';
echo $array_words_1[1];
echo '<br/>';

// Using regular expression. 
$string = "Hello Jerry, Yesterday is a bad day.";
$array_words_2 = preg_split('/\Y/', $string);

var_dump($array_words_2);
echo '<br/>';
echo $array_words_2[0] . '<br/>';
echo $array_words_2[1];

Combine the elements of an array into a string

For this purpose, use can implode function. It allows you to gather all the elements of the array to a string and specifies the separator between the elements when necessary.

  • implode()
// Source Array 
$array = array( 
    'Text', 
    'which the', 
    'Broken', 
    'on', 
    'Elements', 
    'Array');

// Collect the elements of an array into a string 
// And we share each space element 
$string = implode(' ', $array);

// Result output 
echo $string;

Control register, manipulate string

$string = "string two";

// Uppercase first carachter
$ucfirst = ucfirst($string);
echo "ucfirst: $ucfirst<br/>";

// Uppercase first caracte of each word
$ucwords = ucwords($string);
echo "ucwords: $ucwords<br/>";


$string = "string of four words";

$upper = mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
echo "upper: $upper<br/>";


$lower = mb_convert_case($string, MB_CASE_LOWER , "UTF-8");
echo "lower: $lower<br/>";


$case_title = mb_convert_case($string, MB_CASE_TITLE , "UTF-8");
echo "case_title: $case_title<br/>";


$first_char = mb_substr($string, 0, 1, 'UTF-8');
$first_upper = mb_convert_case($first_char, 'UTF-8');
$all_characters = mb_substr($string, 1, mb_strlen($string), 'UTF-8');
$result = $first_upper . $all_characters;

echo "result: $result<br/>";

Output lines in reverse order

$string = "source string";
$strrev = strrev($string);

echo "strrev: $strrev<br/>";

$string = "source string";
$mb_strrev = "";

for($i = mb_strlen($string, "UTF-8"); $i >= 0; $i--) {

    $mb_strrev .= mb_substr($string, $i, 1, "UTF-8");

}
echo "mb_strrev: $mb_strrev";

Determination of the number of substrings

To get the number of substrings in a string you can use the two functions, they both work correctly:

  • substr_count()
  • mb_substr_count()
$string = "Source string for example substring count";

$substr = "string";

$count = substr_count($string, $substr);
$mb_count = mb_substr_count($string, $substr, "UTF-8");

echo "count: $count<br/>";
echo "mb_count: $mb_count<br/>";

Search the position of first occurrence

Search the position of first occurrence may be implemented using one of two php functions:

  • strpos()
  • mb_strpos()
$string = "Source string for example substring count";

$substr = "string";

$count = substr_count($string, $substr);
$mb_count = mb_substr_count($string, $substr, "UTF-8");

echo "count: $count<br/>";
echo "mb_count: $mb_count<br/>";

Reduction of long strings

In the derivation of lines not rarely necessary to accommodate the text in a specific size. However, strings may be of different lengths, including those too large. In such cases it is necessary to shorten the line. And for a more readable type added to the end of the dot. For the implementation it is necessary to receive the length of the string, compare it with the size of the allowable length and crop and ascribe the dots at the end if necessary.

$str = "A string with a very long text which must be shortened !";

$count_max = 15;

$result_str = "";

if(mb_strlen($str, 'utf-8') > ($count_max + 3)) {

    $sub_str = mb_substr($str, 0, $count_max, 'utf-8');

    $result_str = trim($sub_str) . "...";

    $result_str = "<span title='$str'>$result_str</span>";
}
else
{
    $result_str = $str;
}

echo $result_str;