Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - Find An Int in A String?

Options
  • 26-10-2011 10:15am
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    I have a string like this:

    $mystring; //Assigned elsewhere but equals 1234567

    What I want to do is see if a number exists in it. So if I wanted to find if 1 was in the string etc. I've tried converting all to int's and all to strings, but to no avail. In case it matters, $mystring will only ever contain numbers i.e. I won't have to find a number in a string such as "ironclaw27" Any help appreciated!

    ironclaw.

    Some attempts:
    $str = "1";
    $num = (int)$str;
    $mystring =(int)$mystring;
    
    if(stristr($mystring, $num) === TRUE) {
    echo "Found A One";
    }
    
    


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Try strpos to find the first occurrence of the sub string/number. PHP is pretty typeless.
    [php]
    $mystring = "123456";
    $findNum = "1";

    if(strpos($mystring, $findNum)) {
    echo "Found ".$findNum;
    }
    else
    echo "Did not find ".$findNum;
    [/php]


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    There are a few functions here that will help, though I'm not sure what specifically you're trying to do.

    Are you trying to take any number and then locate another number in it?

    So if your number is 612684631, you want to be able to check if there's an "8" in there?

    PHP is effectively typeless so a variable will be whatever you want it to be.

    You can treat the number as a string and use preg_match to locate the other number within it.


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Webmonkey wrote: »
    Try strpos to find the first occurrence of the sub string/number. PHP is pretty typeless.
    [php]
    $mystring = "123456";
    $findNum = "1";

    if(strpos($mystring, $findNum)) {
    echo "Found ".$findNum;
    }
    else
    echo "Did not find ".$findNum;
    [/php]

    That works great except for the number one i.e. $findNum = "1" But I think thats to do with its position isn't it?


  • Registered Users Posts: 8,488 ✭✭✭Goodshape


    ironclaw wrote: »
    That works great except for the number one i.e. $findNum = "1" But I think thats to do with its position isn't it?

    Yeah, the number 1 is in the very first position, so strpos($mystring, $findNum) returns 0, which doesn't pass the if statement.

    Try this instead:

    [php]
    $mystring = "123456";
    $findNum = "1";

    if(strpos($mystring, $findNum) !== FALSE) {
    echo "Found ".$findNum;
    }
    else
    echo "Did not find ".$findNum;
    [/php]


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Ooops apologies! :D


  • Advertisement
  • Registered Users Posts: 297 ✭✭stesh




  • Registered Users Posts: 4,844 ✭✭✭shootermacg


    I would say regular expressions are what you need, easy as pie to find numbers and return them, this is what regular expressions are for.
    Here's a link that should get you cracking.

    http://www.regular-expressions.info/numericranges.html

    Ignore the shell script crap:
     #!/usr/bin/perl
        use strict;
        use warnings;
        my $text = "I have 37 balloons";
    
        $text =~ m/(\d+)/;
        $num = $1;
        print "Bob says that he has $num balloons\n";
    
    
    Take the following expression: (\d+)
    \d means digit and + means 1 or more digits.

    I did php in UCC a long time ago but I know regular expressions are integrated so you would have no issues using them.

    Regular Expression
    Class Type Meaning
    \t Character Set tab
    \n Character Set newline
    \r Character Set return
    \f Character Set form
    \a Character Set alarm
    \e Character Set escape
    \033 Character Set octal
    \x1B Character Set hex
    \c[ Character Set control
    \l Character Set lowercase
    \u Character Set uppercase
    \L Character Set lowercase
    \U Character Set uppercase
    \E Character Set end
    \Q Character Set quote
    \w Character Set Match a "word" character
    \W Character Set Match a non-word character
    \s Character Set Match a whitespace character
    \S Character Set Match a non-whitespace character
    \d Character Set Match a digit character
    \D Character Set Match a non-digit character
    \b Anchor Match a word boundary
    \B Anchor Match a non-(word boundary)
    \A Anchor Match only at beginning of string
    \Z Anchor Match only at EOS, or before newline
    \z Anchor Match only at end of string
    \G Anchor Match only where previous m//g left off

    Actually I googles php reg ex and found the following page, seems like a good resource:

    http://www.webcheatsheet.com/php/regular_expressions.php


Advertisement