简体   繁体   中英

Identify first character in a string

Hello I need help in identifying the first character of a string. if the string contains a forward slash "/" or no forward slash in php.

Thanks in advance

Try this code:

<?php
function containSlash($str)
{
  return substr($str, 0, 1) == "/";
}

// TEST
echo (containSlash("/hello") ? "TRUE" : "FALSE"); // TRUE
echo (containSlash("hello") ? "TRUE" : "FALSE"); // FALSE
?>

Try this

function getSlashes($str)
    {
        return $str[0] ==  '/' || $str[0] ==  '\\';
    }

You're asking two different questions here.

You can examine the first character of a string using

$str[0]

You can check whether a string contains no / by using

strpos($str, '/') === FALSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM