Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string starts with "_" in PHP? [duplicate]

Tags:

php

Example: I have a $variable = "_foo", and I want to make absolutely sure that $variable does not start with an underscore "_". How can I do that in PHP? Is there some access to the char array behind the string?

like image 932
openfrog Avatar asked Dec 25 '09 21:12

openfrog


People also ask

How do I check if a string starts with a specific character in PHP?

To check if string starts with a specific character, use PHP built-in function strpos(). Provide the string and character as arguments to strpos(), and if strpos() returns 0, then we can confirm that the string starts with the specific character, else not.

How do you check if a string contains a specific word in PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do you check if a string ends with a substring in PHP?

To check if string ends with specific substring, use strcmp() function to compare the given substring and the string from a specific position of the string. Take string in variable $string. Take substring in $substring. Compute the length of $substring and store it in $length.

How do you check if a number is double in PHP?

Value Type: Boolean. Example: <? php $var_name=127.55; if (is_double($var_name)) echo 'This is a double value.


2 Answers

$variable[0] != "_" 

How does it work?

In PHP you can get particular character of a string with array index notation. $variable[0] is the first character of a string (if $variable is a string).

like image 138
Peter Porfy Avatar answered Sep 21 '22 10:09

Peter Porfy


You might check out the substr function in php and grab the first character that way:

http://php.net/manual/en/function.substr.php

if (substr('_abcdef', 0, 1) === '_') { ... } 
like image 30
Alex Sexton Avatar answered Sep 22 '22 10:09

Alex Sexton