PHP strings

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

3.8
5
Awgiedawgie 440215 points

                                    
&lt;?php
echo&nbsp;'this&nbsp;is&nbsp;a&nbsp;simple&nbsp;string';

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

//&nbsp;Outputs:&nbsp;Arnold&nbsp;once&nbsp;said:&nbsp;&quot;I'll&nbsp;be&nbsp;back&quot;
echo&nbsp;'Arnold&nbsp;once&nbsp;said:&nbsp;&quot;I\'ll&nbsp;be&nbsp;back&quot;';

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

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

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

//&nbsp;Outputs:&nbsp;Variables&nbsp;do&nbsp;not&nbsp;$expand&nbsp;$either
echo&nbsp;'Variables&nbsp;do&nbsp;not&nbsp;$expand&nbsp;$either';
?&gt;

3.8 (5 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source