...

View Full Version : Checking the first character of a string?



Taipan
03-25-2004, 11:52 PM
Hi,

I need to retrieve a string from a VARCHAR and before I display it I need to check check if the first character is a <

How can I isolate the first character from a string variable?

Thanks in advance.

raf
03-26-2004, 12:18 AM
if ($row['thevar']{0} == '<'){
echo $row['thevar'] ;
}

Taipan
03-26-2004, 12:52 AM
Thanks, I am embarrased to ask such simple things, I do try to search for answers first but I guess I don't search the correct way.

raf
03-26-2004, 08:51 AM
Well, usualy searching he PHP manual at www.php.net delivers a sollution. In this particular case, i don't know if you would have found the above sollution, since it's not such a frequantly used 'thingy'

Most people would use substr($var,0,1) and it's on the substr-page that i recently saw this inside on of the codeexamples.

kSanders
03-30-2004, 11:01 PM
<edit>
stupidity
</edit>

raf
03-30-2004, 11:17 PM
You could also use the strstr() (http://www.php.net/strstr) function without having to use substr. Of course even if it does not start with < but it has < somewhere in the string, it will be true.
:confused: i fail to see the point of your post. strstr() can not be used for this, for exactly the reason you mention. i also don't understand the "without having to use substr" --> is there something problematic with substr() ?
and if you don't want to use it, then
if ($row['thevar']{0} == '<'){
is still the best approach.
So i don't exactly see your point...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum