问题描述:

I have a string of text called doc/document1.pdf. Is there PHP code I can use that will allow me to check if the last 4 characters are equal to '.pdf'?

I am looking for code that looks like this:

<?php if($stringoftext_lastfourcharacters == '.pdf') {

echo "This is a PDF";

}

?>

网友答案:

Use substr() -

if(substr($myString, -4) == '.pdf')....

Or use pathinfo() -

$info = pathinfo($myString);
if ($info["extension"] == "pdf") .... 

You can also use explode() -

$myStringParts = explode('.', $myString);
if($myString[count($myStringParts) - 1] == 'pdf')....
网友答案:
if(substr($str,strlen($str)-3)==="pdf") {...}
网友答案:

Regex way

$reg="/\.pdf$/i";
$text= "doc/document1.pdf";
if(preg_match($reg,$text, $output_array)) { echo "This is a pdf";}
相关阅读:
Top