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
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
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
?>