How to check whether a string starts with another string in PHP

1 Answer

0 votes
function string_start_with($s, $match) {
    $s_len = strlen($s);
    $m_len = strlen($match);
    if ($m_len > $s_len) {
        return false;
    }
    
    return substr($s, 0, $m_len) === $match;
}

$s = "php python c c++ java";

if (string_start_with($s, "php")) {
    echo "yes";
} else {
    echo "no";
}

    
   
           
    
/*
run:
                
yes
         
*/

 



answered Nov 20, 2019 by avibootz

Related questions

2 answers 202 views
1 answer 188 views
3 answers 259 views
1 answer 117 views
1 answer 203 views
...