SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find something in a string

    Hello,
    Thanks for reading this.

    I'm trying to do this:
    One of my pages will get a bunch of posts, to manage them by site, it will get "help mail" id's with 2 chars in front of the id that tag the site example:

    post1: ab2462
    post2: xy3845

    i want it to handle those how do i make an:
    if ($tags == 'xy') {
    // do something
    }

    thanks!

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://php.net/substr

    PHP's substr() is what you're looking for.

    Code:
    $post1 = 'ab2462';
    $id = substr( $post, 0, 2 );
    echo $id; // will echo 'ab'

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    *cough* strpos also
    http://php.net/strpos
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    if (substr($tags, 0, 2) == 'xy') {
    // do something
    }

  5. #5
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    if (substr($tags, 0, 2) == 'xy') {
    // do something
    }
    This is generally bad practice. If you use more than one if statement, it performs substr() as many times as there are if statements


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
  •