Here you can see how to remove unnecessary whitespace and specific characters from the beginning and end of strings using PHP’s trim(), ltrim(), and rtrim() functions.
Code:
Continue reading “PHP String Trimming Functions”Simple Web Development tips & spiritual observations
Here you can see how to remove unnecessary whitespace and specific characters from the beginning and end of strings using PHP’s trim(), ltrim(), and rtrim() functions.
Code:
Continue reading “PHP String Trimming Functions”Here I have shown how to efficiently replace multiple words in a string using PHP’s str_replace function. In this example you can see how to modify text by swapping out specified words with new ones.
CODE:
Continue reading “Efficiently Replacing Multiple Words in a String Using PHP”The spaceship operator (<=>) in PHP is used for comparing two values and returns -1, 0, or 1 depending on whether the first value is less than, equal to, or greater than the second value. Introduced in PHP 7, it simplifies comparisons and is especially useful for sorting functions.
CODE:
Continue reading “Understanding PHP Spaceship Operator for Comparing Values”stripos searches for the first occurrence of a substring in a string, ignoring case.strripos searches for the last occurrence of a substring in a string, ignoring case.CODE:
<?php
$string = "This is a test by Tom. He is a good boy. This is another test";
echo "Main sentence is:<br>";
echo $string;
echo '<br>Search the word "test" using strpos() <br>';
echo strpos($string, "test");
echo '<br>Search the word "Test" using strpos() <br>';
echo strpos($string, "Test");
if(strpos($string, "Test")){
echo "Word Found";
}else{
echo "Word not found--as strpos() is case sensitive";
}
echo '<br>Search the word "Test" using stripos()-- case sensitive <br>';
echo stripos($string, "Test");
echo '<br>Search the word "This" using stripos()-- case sensitive <br>';
echo stripos($string, "This");
echo "<br>";
if(stripos($string, "This") !== false ){
echo " Word is found";
}else{
echo "Word not found";
}
echo '<br>Search the word "This" using strripos()-- case sensitive. <br>';
echo strripos($string, "This");
echo "<br>";
echo "Another Test<br>";
$text = "This is a test. This is only a test.";
echo $text;
echo "<br>";
$substring = "test";
$firstPos = stripos($text, $substring);
$lastPos = strripos($text, $substring);
echo "First occurrence of test: " . $firstPos . "\n";
echo "Last occurrence of test: " . $lastPos . "\n";
?>
CODE:
<?php
$sentence ="Hello World, My name is Jimmy";
echo "split using explode<br>";
$split1= explode(" ",$sentence);
print_r($split1);
echo "<br>Join Splitted Sentence<br>";
$join1=join(" ",$split1);
echo $join1;
echo "<br>Join splitted sentence using implode<br>";
$join2 = implode(" ",$split1);
echo $join2;
echo "<br>Split string using multiple delimeter<br>";
$split2 = strtok($sentence," ,");
while ($split2 !== false){
echo $split2."<br>";
$split2=strtok(" ,");
}
print_r($split2);
echo "<br>Split string using regular expression <br>";
$split3= preg_split("/ |,/",$sentence);
print_r($split3);
echo "<br>Split \"Hello World\" in characters<br>";
echo <<<MyText
Split "Hello WOrld" in Characters <br>
MyText;
$split4 = str_split("Hello World");
print_r($split4);
?>
CODE:
<?php
$sentence = "My name is Jon";
echo "<br>Main sentence is:<br>";
echo $sentence;
$rev1= strrev($sentence);
echo "<br>First Reversal:<br>";
echo $rev1;
echo "<br>2nd Reversal system: <br>";
$rev2= "";
for($i=1;$i<=strlen($sentence);$i++){
$rev2.=$sentence[$i * -1] ;
}
echo $rev2 ;
$rev3="";
echo "<br>3nd Reversal system: <br>";
for($i=strlen($sentence);$i>=0;$i--){
$rev3.= $sentence[$i];
}
echo $rev3;
?>
CODE:
String in PHP heredoc and nowdoc
<?php
$beVerb = "is";
$sentence1 = 'My name $beVerb Jim';
echo "Sentence 1 in single quotation";
echo "<br>";
echo $sentence1;
echo "<br>";
$sentence2 = "My name $beverb Jim";
echo "Sentence 1 in single quotation<br>";
echo $sentence2;
echo "<br>PHP HERE DOC EXAMPLE<br>";
$str = <<<tom
the beverb i used $beVerb
another test
'this is multiple test'
and the be verb is $beVerb
another "test"
tom;
echo $str;
echo "<br>PHP NOW DOC EXAMPLE<br>";
$str = <<<'tom'
the beverb i used $beVerb
another test
'this is multiple test'
and the be verb is $beVerb
another "test"
tom;
echo $str;
?>
CODE:
<?php
$name=array("Bobby", "Robby","James","Tobby","Inaya","Jack");
echo "Main Array at the begining <br>";
print_r($name);
echo "<br>New array after Splice<br>";
$newEntry=array("Robin","Tony");
$name1 = array_splice($name,2,-1,$newEntry);
print_r($name1);
echo "<br>Main array after Splice including new entry<br>";
print_r($name);
?>
From the screenshot you can see, i am showing the google map using shortcode
[acf field="maps"]

Code:
<?php
$name= array("Tom", "Jon","Rowan","Jim","Harry","Rasha");
echo "Main Array";
echo "<br>";
print_r($name);
echo "<br>";
echo "slice upto 3";
echo "<br>";
$name1=array_slice($name,3);
print_r($name1);
echo "<br/>";
echo "Keeping Key same: slice 3,2";
$name2=array_slice($name,3,2,true);
print_r($name2);
echo "<br/>";
echo "slice 3,-1<br>";
$name3= array_slice($name,3,-1);
print_r($name3);
echo "<br>slice 3,-2<br>";
$name3= array_slice($name,3,-2);
print_r($name3);
echo "<br>slice -3,-2<br>";
$name3= array_slice($name,-3,-2);
print_r($name3);
echo "<br>slice -3,-1<br>";
$name3= array_slice($name,-3,-1);
print_r($name3);
echo "<br>slice -3,1<br>";
$name3= array_slice($name,-3,2);
print_r($name3);
?>