jalf
jalf

Reputation: 585

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

Upvotes: 0

Views: 1269

Answers (3)

Moyed Ansari
Moyed Ansari

Reputation: 8461

Try this

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

Upvotes: 1

Mattias Farnemyhr
Mattias Farnemyhr

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

Joey
Joey

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

Related Questions