Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php string functions

#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;
Comment

PHP Strings

<?php
echo strlen("Hello world!"); // outputs 12
?>
Comment

string function in php

<?php
    $x = 'Hello';
    print "Length of string is: " . strlen($x);
    print "<br>Count of word: " . str_word_count($x);
    print "<br>Reverse the string: " . strrev($x);
    print "<br>Position of string: " . strpos('Have a nice day!', 'nice');  //2 argument
    print "<br>String replace: " . str_replace('good', 'nice', 'have a good day!');  //3 argument
    print "<br>String convert to uppercase: " . strtoupper($x);
    print "<br>String convert to lowercase: " . strtolower($x);
    print "<br>convert first character into uppercase: " . ucfirst('good day');
    print "<br>convert first character into lowercase: " . lcfirst('Good noon');
    print "<br>convert first character of each word into uppercase: " . ucwords('keep going on!');
    print "<br>Remove space from left side: " . ltrim("        hi..");
    print "<br>Remove space from right side: " . rtrim("hello          ");
    print "<br>Remove both side of space: " . trim("       keep learning       ");
    print "<br>string encrypted with MD5: " . md5($x);
    print "<br>Compare both string: " . strcoll('Hello', 'Hello') . "<br>" . strcmp('kinjal', $x);
    print "<br>Return part of string: " . substr('Hello Everyone', 2);
    ?>
Comment

PHP strings


<?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: 
 a newline
echo 'This will not expand: 
 a newline';

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

Comment

strings php

returning portion of a string
Comment

strings functions php

check out php manual > https://www.php.net/manual/en/ref.strings.php
Comment

PREVIOUS NEXT
Code Example
Php :: php add 1 day hours to unix timestamp 
Php :: Write a php program to swap two numbers using temporary variable 
Php :: Turn error log WP 
Php :: what does = mean in php 
Php :: how to grab shortcode from custom post type 
Php :: psr/log is locked to version 2.0.0 and an update of this package was not requested. - psr/log 2.0.0 requires php =8.0.0 - your php version (7.4.26) does not satisfy that requirement. 
Php :: laravel create custom artisan command 
Php :: can i install php7.4 inside vagrant homestead 
Php :: dateinterval hh mm ss 
Php :: Warning: Undefined array key "playerthrows" in C:xampphtdocsmini_projetindex.php on line 10 
Java :: java get screen size 
Java :: import android.support.v7.app.appcompatactivity error 
Java :: how to set current date in android studio 
Java :: import javax.validation.valid error 
Java :: regex java email validation 
Java :: how to loop through code 3 times java 
Java :: install java 11 from centos 7 
Java :: java convert bytes to string 
Java :: return boolean value from stream 
Java :: filll 2d array in java 
Java :: open webpage android studio 
Java :: array to map java3 
Java :: JFrame Exit oon close Java15 
Java :: how to parse double upto 2 decimal in java 
Java :: bukkit runnable repeating scheduler 
Java :: HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to ztdev.co.za not permitted by network security policy 
Java :: java how to get elapsedTime 
Java :: how to get time in dd/mm/yyyy format in java 
Java :: leap year program in java 
Java :: android hide textview 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =