Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: compare substrings

Really quick noob question with php:

I'm trying to create a conditional statement where I can check if a variable partially matches a substring.

More specific in my case:

$cal is the name of a file, I want it so if $cal contains ".cfg" it echos some text.

pseudocode:

<?php if ($cal == "*.cfg")
 {
    echo "Hello ";
 }
?> 
like image 749
dukevin Avatar asked Aug 30 '26 13:08

dukevin


1 Answers

if (strpos($cal, ".cfg") !== false)
{
  echo "some text";
}

EDIT

My proposal for matching exactly "*.cfg":

$cal = "abc.cfgxyz";
$ext = "cfg";

if (strrpos($cal, ".".$ext) === strlen($cal)-strlen($ext)-1)
{
    echo "matched";
}
like image 189
Frosty Z Avatar answered Sep 01 '26 03:09

Frosty Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!