How To Extract Number

Discussion in 'PHP' started by deriklogov, Jul 18, 2009.

  1. #1
    Hey, I got part of text , every time different but looks like this

    '/s4w/1276675395.html'

    how to extract number (in this case) 1276675395 ?

    I used preg_replace "/[^0-9]/i" , but then I figure out that in s4w number 4 is present so its doesnt work.
     
    deriklogov, Jul 18, 2009 IP
  2. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i have this simple function, it might help :)

    function get_string_between($string, $start, $end){
            $string = " ".$string;
            $ini = strpos($string,$start);
            if ($ini == 0) return "";
            $ini += strlen($start);   
            $len = strpos($string,$end,$ini) - $ini;
            return substr($string,$ini,$len);
    	}
    
    
    
    echo get_string_between('/s4w/1276675395.html', '/s4w/', '.html'); 
    PHP:
    This should print out
    1276675395


    best luck :cool:
     
    zeronese, Jul 18, 2009 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    $num = preg_replace('#^.+/(\d+)\.html$#i', '$1', $str);
    Code (markup):
     
    joebert, Jul 18, 2009 IP
  4. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,080
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Thank You Very Much
    Works like a charm
     
    deriklogov, Jul 18, 2009 IP
  5. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Regex is a Swiss army knife but sometimes the overhead is not needed when a simple string function will work:
    
    $var = basename('/s4w/1276675395.html', '.html');
    
    echo $var; // 1276675395
    
    PHP:
     
    Chemo, Jul 19, 2009 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    I've also made a similar function :D it's optimized to the max i think.

    
    function between($source, $start, $end, $p = 0) {
    	$s = strpos($source, $start, $p) + strlen($start);
    	return substr($source, $s, strpos($source, $end, $s) - $s);
    }
    
    PHP:
     
    Kaizoku, Jul 19, 2009 IP
  7. mushu

    mushu Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    function extractOnlyNumbers($source) {

    $result = "";
    $Letter = "";
    $valid_chars ="1234567890";

    for ($i = 0; $i < strlen($source); $i++) {
    $Letter = substr($source, $i, 1);
    if (strstr($valid_chars, $Letter) !== FALSE)
    $result .= $Letter;
    }
    return $result;
    }
     
    mushu, Jul 20, 2009 IP
  8. tech36.com

    tech36.com Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This should probably do the trick.
     
    tech36.com, Jul 20, 2009 IP