happy cat image

Start Web Coding

Your First step toward web coding,
Start Web Coding

change UI

Function 3

substr(), strchr() Function to Cut a String

Truncates string substr(string, string array to trim, length
of string ) strchr(string, string to begin truncation ) Strchr(string, string to begin truncation
) Set the position to start with from 0 to 1, starting with 0,1,2,3,4,5 ..., and the strchr() function marks the position as the character of the string to start cutting.
When specifying the number of string characters, English and symbols are 1 byte per character, so the array number is set in order of 0,1,2,3,4,5, but
Korean is 2 bytes, so 0,1 2,3 4,5 You must specify the array with.

For example,
$a = "Let's work hard to get rich";
$b = "Let's work hard and make your dreams come true";

$str [0] = substr($a, 6); // truncate the sixth variable a and print it out Hard work 01 23 45 It takes up 6 bytes and prints the rest except hard work.
$str [1] = substr($a, 7, 7); // print hard, including spaces
$str [2] = substr($a, -9); // go in reverse At the end of the marriage, we'll print until we get married, including a space.
$str [3] = substr($a, -16, 6); // go back and print the beauty and

$str [4] = strchr($a, "effort"); Print out the effort first.
$str [5] = strchr($b, "dream"); Output your dreams first. Chop()-removes

whitespace from
the end of a string and
trim()-removes whitespace before and after a string .
ltrim()-Remove Leading Spaces
rtrim()-Remove Leading Spaces An

array of strings. explode() This function creates an array based on the spaces in the string.
If you look at how to use the source below

<?php
    $a = "Hello my name is David.";
    $b = explode(" ",$a); //The first argument is a space delimiter and the second is an object.
    $s = 0;
    for($c = 0; $c <= 4; $c++){
        $s+=1;
        echo "$s. $b[$c] <br />";
    }
?>

An array is declared with the name of $b with explode, which means that the value of $b [0] is hello.
In explode(), the first value puts a space and separates it.
If so , put the character you want, recognize it and specify the array.
This can be easily understood by changing the first argument of explode by specifying your own $a value.
String change function: str_replace() function
str_replace("object", "replace character", target variable)

<?php
    $a = "Hello my name is David.";

    $b = str_replace("David","Asimo", $a); //replace david in $a with asimo 
echo " $b <br />"; ?>

String search function: preg_match()
This function returns 1 if the string to be found in any string exists and 0 if it does not exist.
preg_match(/ string to find /, string to search)

<?php
    $a = "Hello I'm David.";

    $b = "/I/";

    $c = preg_match($b, $a);

    echo " $a  <br />";

    if($c){
        echo "Search Result: {$b} Found <br />";
    }else{
        echo "Failed to find.";
    }
?>

String comparison function: strcmp()

This function compares two strings according to the size of the ASCII code value of the first character of the two strings.
$a> $b Returns 1 if true
$a = $b returns 0
$a <$b returns -1


Backslash insertion and removal:
addslashes() -Inserts backslash in quoted string
stripslashes()-Remove backslash

$a = "you're my angel ";
$b = "you\re my angel ";
$add = addslashes($a);
echo " $add ";

$sub = stripslashes($b);

echo " $sub ";

강좌로 돌아가기