Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Checking the first character of a string?

    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.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if ($row['thevar']{0} == '<'){
         echo 
    $row['thevar'] ;

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    95
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    New to the CF scene
    Join Date
    Mar 2004
    Location
    Florida
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <edit>
    stupidity
    </edit>
    Last edited by kSanders; 03-30-2004 at 11:37 PM.
    [ kSanders ] || [ PHP.net ] || [ PostgreSQL ] || [ RTFM!! ] || [ Slackware ]
    "Once you go slack, you never go back"
    "Talk is cheap. Show me the code." - Linus Torvalds

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kSanders
    You could also use the 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.
    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...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •