SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strpos first few characters

    Happy Holidays!

    I have this script, it works. But I would like it to work better.
    Here it is:

    PHP Code:
    if (strpos($item_number"BBB") === false) {
    $description $description;
    $title $title;
    } else {
    $description "$description blank book";
    $title "Blank Book $title";

    The thing is that it works for all my items that are lettered BBB*
    but now it's also doing it for items that have the BBB in it. LIke:

    RGBBBTW..

    Is there a way that I can change the script to only go by the first characters and not the after the last character?

    I am not sure if this is even possible. I did search the net but to no avail.

    Thanks,
    Kevin
    I PHP!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strpos()
    Quote Originally Posted by phpman
    Returns the numeric position of the first occurrence of needle in the haystack string.
    So, if the string is found at the start, the return value will be 0(the first character in a string is position 0).

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Wait, if the string is exactly 'BBB' then why not simply test $string == 'BBB'?

    If you want to test the first three letters of a string you can use substr() instead.

    PHP Code:
    if (substr($string03) == 'BBB'

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The if/else is not needed here, given your code sample, you only need to perform an actions when the 'BBB' string prefixes the $item_number.

    PHP Code:
    <?php
    if(=== strpos($item_number"BBB")){
        
    $description "$description blank book";
        
    $title "Blank Book $title";
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict Kevinslair's Avatar
    Join Date
    Jul 2002
    Location
    Binghamton
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you that works 110%

    Thanks!!!! Happy Holidays!
    KEvin
    I PHP!


Tags for this Thread

Bookmarks

Posting Permissions

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