Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
Length of last word CODE
<?php
function lengthOfLastWord1($s) {
        $words = str_word_count($s,1);  //returns words in array
        //(function returns an array with the words from the string) 
        $wordCount = str_word_count($s); //counts words in text 
        $lastW =  $words[$wordCount-1];//returns last word
        $lastWLen = strlen($lastW); //The last word length
        $lastResult = "The last word is ".'"'.$lastW.'"'." with length $lastWLen";
        return  $lastWLen;
        return $lastResult;      
by Guram Azarashvili
2 years ago
0
PHP
Solutions
0
Remove element without loop CODE
// The array_diff() function compares the values of two (or more) arrays, and returns the differences. // Input: nums = [0,1,2,2,3,0,4,2], val = 2 // output: nums = [0,1,3,0,4]
function removeElement($nums, $val) {
    return array_diff($nums, [$val]);   
}
print_r(removeElement([0,1,2,2,3,0,4,2], 2);
// runtime 7ms
by Luka Khitaridze
2 years ago
0
PHP
Array
0
sqrt() and floor()
function mysqrt($x){ $x=sqrt($x); $x=floor($x); return($x); } echo mySqrt($x);
by Anna Tukvadze
2 years ago
0
PHP
Object Oriented PHP Tutorial
0
array_unique($arrary) function;
1. <?php function removeDuplicates($nums){ $nums=array_unique($nums); return $nums; } print_r (removeDuplicates([3, 5, 1,2,3, 5])); ?> 2. <?php function removeDuplicates($nums){ $result=[]; foreach ($nums as $num){ if (!in_array($num, $result)){ $result[]=$num; } } return $result; } print_r (removeDuplicates([3, 5, 1,2,3, 5])); ?>
by Anna Tukvadze
2 years ago
0
PHP
26. Remove Duplicates from Sorted Array
Object Oriented PHP Tutorial
0
function is_multi($array) {
    $filter = array_filter($array, 'is_array');
    print_r($filter);
    if(count($filter) > 0) {
    	return true;
    }
    return false;
}
var_dump(is_multi([
	"Valeri"=> [
		"weight"=>77, 
	],
	"Giorgi"=> [
		"weight"=>87, 
		"parents"=> [
			"father"=>"Daviti",
			"mother"=>"Mariami",
		]
	],
]));
by Luka Khitaridze
2 years ago
0
PHP
Array
0
╰┈➤ When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument
function print_string(&$string) {
    $string = "Function string \n";
    echo($string);
}
$string = "Global string \n";
print_string($string);
echo($string);
// outputs: Function string, Global string
► Scope of both global and function variable becomes global as both variables are defined by same reference. Therefore, whenever global variable is change, variable inside function also gets changed. ◄
If we delete & (ampersand) symbol before variable, it outputs: Function string, Global string
by Luka Khitaridze
2 years ago
0
PHP
Variable
0
╰┈➤ The str_replace() function replaces some characters with some other characters (together) in a string. For example :
$test = "Hello world";
echo str_replace("world", "Valer", $test);
// outputs: Hello Valer
► str_replace() is a more global approach to replacements, while
strtr() simply translates the characters ONE BY ONE.
// therefore if we use that function instead of str_replace at that case, it outputs differently. ◄
$test2 = "Hello world";
echo strtr($test2, "world", "Valer");
// outputs: Heeea Valer
// because it translated l > e, o > a. (w > V, r > l, d > r)
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
╰┈➤ The array_keys() function returns an array containing the keys.
function arrayKeys($array) {
    print_r($array);
    return array_keys($array);
}
print_r(arrayKeys(["House" => "White", "Car" => "Black"]));
// testing print_r outputs: ( [House] => White, [Car] => Black ) // The function returns: ( [0] => House, [1] => Car )
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
╰┈➤ array_fill_keys() function fills an array with values, specifying keys.
function arrayFillKeys($array) {
    print_r($array);
    $array = array_fill_keys($array, 'value');
    return ($array);
}
print_r(arrayFillKeys([4,3,2]));
// testing print_r outputs: ( [0] => 4, [1] => 3, [2] => 2 ) // The function returns: ( [4] => value, [3] => value, [2] => value )
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
╰┈➤The preg_match() function returns whether a match was found in a string, preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or false on failure (error).
$string = "hello world";
$pattern = "/world/";
echo preg_match($pattern, $string);
// outputs 1
► As usual, we use this function together with Regex (Regular Expression) syntax and we adapt it to the different solutions. for example, in regular expression "i" means case insensitive check. ◄
therefore if that case would be like that:
$string = "Hello wORLD";
$pattern = "/world/i";
echo preg_match($pattern, $string);
// it would still outputs 1
► Valeri has used this function in a task to check for the only uppercase letters, only lowercase letters and first uppercase letter in the string via regex. ◄
For example: (via Regex - Regular Expression and preg_match)
(^[A-Z]*$) it means if there are only uppercase letters in a string.
(^[a-z]+$) it means if there are only lowercase letters in a string.
(^[A-Z]{1}[a-z]*$) it means if the first letter is uppercase in a string.
| - it means "Logical or" via regex as well.
by Luka Khitaridze
2 years ago
0
PHP
Functions
0
Results: 1580