What are String Functions in PHP?

Learn to use string functions in PHP, including strlen(), substr(), and strpos(). PHP provides a number of functions for manipulating strings. These functions are designed to avoid the manual process of concatenating together individual strings and to avoid common security problems that occur with string concatenation.

php string functions

By JSMDJSMD on May 13, 2020
#String Functions

substr()  #Returns a portion of a string
===========
<?php
    #substr()  Returns a portion of a string
    $output = substr('Hello', 1, 3);
    $output1 = substr('Hello', -2);//starts from the back of the string
    echo $output;
    echo '<br>';
    echo $output1;
?>
===============
strlen() #Returns the length of a string
===============
    $output = strlen('Hello');
    echo $output;
?>
===============
strpos() #finds the position of the first occurence of a sub string
===============
    $output = strpos('Hello World', 'o');
    echo $output;
    $output1 = strrpos('Hello World', 'o'); #last occurance
    echo $output1;
================
trim()  # trims white space
================
 $text = 'Hello World                ';
    var_dump($text);
    echo '<br>';
    $trimmed = trim($text);
    echo $trimmed;
    echo '<br>';
    var_dump($trimmed);
==================
strtoupper() # makes everything uppercase
==================
$text = 'Hello World';
    $uppercase = strtoupper($text);
    echo $uppercase;
==================
strtolower() #makes everything lowercase
==================
  $text = 'Hello World';
    $lowercase = strtolower($text);
    echo $lowercase;
==================
ucwords() #Capitalizes every word
===================
    $text = 'hello world';
    $proppercase = ucwords($text);
    echo $proppercase;
==================
str_replace() #Replace all occurances of a search string 
              #with a replacement
==================
$text = 'hello world';
    $wordreplace = str_replace('world', 'john', $text);
    echo $wordreplace;
=================
is_string() #Checks to see if it is a string
=================
    $val = 'Hello';
    $output = is_string($val);
    echo $output;
    echo '<br>';

    $values = array(true, false, null, 'abc', 33, '33',
    22.4, '22.4', '', ' ', 0, '0');

    foreach($values as $value){
        if(is_string($value)){
        echo "{$value} is a string<br>";
    }
}
=================
gzcompress() # Compress a string
=================
    $string = 
    "a;laksd;lk;lkasd;lkas;lk;lkd;lkasd;lka;lskd;lka;lkd;lk
    as;l;laksd;lk;lkasd;lkas;ldk;laskd;lakd;lkad;l
    adslkjlkasjdlkjlkjaslkjaslkdjlkajdlkajdlkajd
    alskdjlkasjdlkjadlkjadlkjadlkjadlajd
    adlkjlkjalksjdlkjlkjlkjklajsda";

    $compressed = gzcompress($string);
    
    echo $compressed;
    echo '<br>';

    $original = gzuncompress($compressed);

    echo $original;

Add Comment

4

PHP strings

By Black BeetleBlack Beetle on Jun 25, 2020
<?php
echo 'this is a simple string';

echo 'You can also have embedded newlines in
strings this way as it is
okay to do';

// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';

// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>

Source: www.php.net

Add Comment

0

strings php

By Dark DotterelDark Dotterel on Jul 03, 2020
returning portion of a string

Add Comment

0

String functions provide a set of tools to manipulate and return strings in PHP. And then goes on to describe the most commonly used string functions.

PHP answers related to "php string functions"

View All PHP queries

PHP queries related to "php string functions"

php string functions how to convert array to string with commas in php php string to date Check if string starts with php Convert string to lowercase in php date to string php ?? ' ' operator in php laravel php artisan app:name in laravel 6 php artisan make:auth laravel 8 not working php artisan ui:auth laravel 7 php cache clear in laravel php extensions for laravel php laravel dump php laravel return json response php laravel rount price to 99 php run server laravel php unit testing laravel remove index.php laravel in linux server cheque print in php codeigniter install php ubuntu 20.04 php enable module php display errors php console log php for loop php sha256 php in array php uppercase each word php uppercase first letter php is numeric php now simple localhost php php include once inside a function? php if else curl php example Line : 83 -- syntax error, unexpected end of file php php file_put_contents pagination in php php print_r php check if type is mysqli_result saber value ao escolher combobox php Best Code Beautifier tools for php Uncaught TypeError: Argument 1 passed to NotORM_Result::update() php slim array shift php php PDO database connection set default php version in ubuntu php time() spread operator php radio button in php form increase PHP Memory login.php php class https//:localhost/phpmyadmin/index.php php get session variable from table parseint php Php array length php reverse shell Mkdir permission denied php Php += Move uploaded file in php Php mysqli fetch assoc Php trying to get property of non-object Php strtolower date format in php array to object in php Send email php smtp hostinger Show all errors in php binemon to php password and confirm password validation in php php int to strin php timeout Php localhost str_pad_left in php php htmlspecialchars php redirect to page remove invalid characters from a string laravel length of string in javascript

Browse Other Code Languages

CodeProZone