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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts

    search string for a phrase

    How do I check if a string contains a phrase? I'm looking for INVALID in a string. What I want to do exacly is if a string contains INVALID then it erases everything but the INVALID

    example:

    88fdfdINVALIDegr // becomes INVALID

    INVALID // becomes INVALID

    randomtextINVALID // becomes invalid



    thanks..
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nobody responds?? Isn't it just something like
    PHP Code:
    $tringtosearchandtransform '88fdfdINVALIDegr';
    if (
    stristr('INVALID'$tringtosearchandtransform) !== FALSE ){
       
    $tringtosearchandtransform 'INVALID';
    }
    echo 
    $tringtosearchandtransform
    (case sensitive)
    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
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Close but no dice
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I figured this out myself. I used preg_match();

    Code:
    <?php
    $string  =  "LALAlalala7r64INVALD373";
    if (preg_match("/INVALID/i",  $string)) 
    {
    	$string  =  "INVALID";
    }
    else 
    {
    }
    print  $string;
    ?>
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #5
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, don't forget that you're adding the /i switch to the RegEx. this makes the search case-INsensitive.

    it will match "Invalid", "InVaLiD", or any other messed up stuff you can think of.

  • #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 DesignersToolz
    Close but no dice
    Yess, should have been
    PHP Code:
    if (stristr($tringtosearchandtransform'INVALID') !== FALSE ){
           
    $tringtosearchandtransform 'INVALID';

    PHP isn't realy consistent about the haystack-needle attributes order
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried reversing the paremeters once but with no luck. ( I had changed some other things to so..)
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #8
    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 DesignersToolz
    I tried reversing the paremeters once but with no luck. ( I had changed some other things to so..)

    Just tried it and it worked. Anyway, there are about a dozen ways to do this so take your pick.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    New to the CF scene
    Join Date
    Apr 2004
    Location
    Indonesia
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <?php
    $input_string='akfhi758q2 345kINVALIDjksey5829';
    $string = 'INVALID';
    $checkstring = strstr($input_string, $string);
    if(strlen($checkstring)!=0)
    {echo $string;
    }

    ?>

  • #10
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just tried it and it worked. Anyway, there are about a dozen ways to do this so take your pick.


    Thats why I said i had changed some other things to heh.
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would use:

    PHP Code:
    <?php
    if(stripos($string'invalid') !== false){
       
    $string 'INVALID';
    }
    echo 
    $string;
    ?>
    but like raf said there are several ways of doing it.
    Last edited by missing-score; 04-03-2004 at 12:32 AM. Reason: slight syntax error


  •  

    Posting Permissions

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