Reputation: 585
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
Upvotes: 0
Views: 1269
Reputation: 8461
Try this
function getSlashes($str)
{
return $str[0] == '/' || $str[0] == '\\';
}
Upvotes: 1
Reputation: 4238
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
?>
Upvotes: 3
Reputation: 354506
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
Upvotes: 0