Web Development & WordPress

Mastering PHP String Search: The Differences Between strpos, stripos, and strripos Explained

  • 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";    
?>
Continue reading “Mastering PHP String Search: The Differences Between strpos, stripos, and strripos Explained”
Web Development & WordPress

Split String or String tokenization

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

?>
Continue reading “Split String or String tokenization”
Web Development & WordPress

Reverse String in various ways

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;
?>
Continue reading “Reverse String in various ways”
Web Development & WordPress

String in PHP heredoc and nowdoc

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;

?>
Continue reading “String in PHP heredoc and nowdoc”
Web Development & WordPress

Array Splice to cut data from main array and insert new data : PHP

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);
?>
Continue reading “Array Splice to cut data from main array and insert new data : PHP”
Web Development & WordPress

Array slice to extract data in php

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);
?>
Continue reading “Array slice to extract data in php”
Web Development & WordPress

JSON to php object and array

Code:

<?php
class StudentInfo{
    public $name;
    public $age;
    private $mobileNum;
    public  function setMobileNum($mobileNum){
        $this->mobileNum=$mobileNum;
    }
    public function getMobileNum(){
        return $this->mobileNum; 
    }
}
$student = new StudentInfo();
$student -> name ="Tommy Jones";
$student -> age =45;
print_r($student);
$student->setMobileNum('11111111');
echo "<br>";
print_r($student);
$jsonEncoded=json_encode($student);
echo "<br>";
print_r($jsonEncoded);
$jsonDecoded=json_decode($jsonEncoded);
echo "<br>";
print_r($jsonDecoded);
$jsonDecodedArray=json_decode($jsonEncoded,true);
echo "<br>";
print_r($jsonDecodedArray);
?>
Continue reading “JSON to php object and array”