Page 1 of 1
How to find specific number of characters after something ?
Posted: Thu Jan 22, 2009 8:08 pm
by lovelf
Example: "i: 12345"
"12345" is dynamically written in a file to be included so I need to find the first 6 characters after "i:" so I can get:
char 1 = " ", char 2 = "1", char 3 = "2" and so on for the first 6 characters after a unique "i:" in the document that is being included where the numbers after i:, are dynamically generated and there could be up to 99999 different values.
I need to get that dynamically generated number, I have been tried to find a solution for days and could not find one yet.
This is what I have so far:
Code: Select all
$str = "i: $dynamic_number";
preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
I still need to make $dynamic_number the number after "i:" to be able to use that code.
Re: How to find specific number of characters after something ?
Posted: Fri Jan 23, 2009 3:55 am
by Mark Baker
Look at the PREG_OFFSET_CAPTURE flag for preg_match(). That returns the position within the string of each match found
Re: How to find specific number of characters after something ?
Posted: Fri Jan 23, 2009 4:14 am
by mattpointblank
Can't you just use substr()?
$dynamicNumber = substr('i: 12345', 3); // returns 12345
As long as you always have 3 characters ('i: ') before the number, that should work fine.
Re: How to find specific number of characters after something ?
Posted: Sun Jan 25, 2009 7:12 pm
by lovelf
The issue is I do not know the value of the number, it could be anything, from one to 99999, all I know is the location of it, which is after "i: "
Re: How to find specific number of characters after something ?
Posted: Sun Jan 25, 2009 8:18 pm
by John Cartwright
strpos() + strlen(), or preg_match(,,,PREG_OFFSET_CAPTURE) (as Mark Baker pointed out)
Re: How to find specific number of characters after something ?
Posted: Sun Jan 25, 2009 9:49 pm
by lovelf
OK, I think I got it but how do I make the entire document that I am including via "include();" the place to be looked for by preg_match ?
How do I "tell" the preg_match function to look for "i: " in the document ?
Thanks for helping me.
Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 3:00 am
by mattpointblank
lovelf wrote:The issue is I do not know the value of the number, it could be anything, from one to 99999, all I know is the location of it, which is after "i: "
That shouldn't make a difference to the code I posted, it will return anything after the "i: ".
Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 3:43 am
by lovelf
OK, so this is what I do:
Code: Select all
$dynamicNumber = substr('i: ', 3);
and I get for the value of $dynamicNumber nothing, so I must be doing something wrong.
Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 4:34 am
by mattpointblank
That's not the correct usage, it's more like:
Code: Select all
$code = "i: 123456";
$dynamicNumber = substr($code, 3);
Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 4:38 am
by lovelf
I see, but I do not know the number for $code, that is what I am trying to retrieve somehow.
The number gets dynamically generated from another server every 24 hours, actually I need to retrieve 4 different numbers on 4 different documents that change over 24 hours, all I know is they are located after "i: ", how could I get such number, if possible at all ?
Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 5:36 am
by lovelf
Simpler, a function to retrieve any number in a string?
Like, if there is a number in the string, then assign a variable for that number.
If a number was found, 0-9 different than a-z and symbols store it as a variable, could that be done with PHP?
Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 6:10 am
by lovelf
I am almost there, I will post the solution in case someone finds it useful in the future

Re: How to find specific number of characters after something ?
Posted: Mon Jan 26, 2009 6:33 am
by lovelf
Here is the solution, it involved file_get_contents, ereg_replace, str_replace, strlen, str_split, if else, variable assignment, and echo.
Code: Select all
<?php
$str=file_get_contents("http://website-hosted-on-another-server-that-generates-a-precious-dynamic-number.com/");
$tn = ereg_replace("[^0-9]", "", $str ); // strip out everything but numbers on document
$tn = str_replace('34018859194403228992002001000', "", $tn); // some left over numbers that are always the same from the source code that must be erased
$tnv = strlen($tn); // get the lenght of the resulting number to make a new variable equivalent to the containing "hundred" of the number, if the number was 8120, the new variable would be 8100
$tnb = str_split($tn); // make an array with the resulting number, the number should be in the parameter of 1-99999 with two extra zeros that could not be erased from the source code that are after the number in the parameter
$tn1 = $tnb[0]; // lets assign a variable to each number in the string up to the first 5 numbers since the last two are the zeros that remained
$tn2 = $tnb[1];
$tn3 = $tnb[2];
$tn4 = $tnb[3];
$tn5 = $tnb[4];
$tn6 = "00"; // this one is to make the variable that contains the lower closest containing "hundred"
if ($tnv == 7) //10000-99999
{$ptn = $tn1.$tn2.$tn3.$tn6; $ptnv = $tn1.$tn2.$tn3.$tn4.$tn5;} // ptn would be the closest containing hundred, and ptnv would be the number I have been looking for days
else if ($tnv == 6) //1000-9999
{$ptn = $tn1.$tn2.$tn6; $ptnv = $tn1.$tn2.$tn3.$tn4;}
else if ($tnv == 5) //100-999
{$ptn = $tn1.$tn6; $ptnv = $tn1.$tn2.$tn3;}
else {$ptn = 0; $ptnv = $tn1.$tn2;} //0-99
echo "$ptn";
echo "$ptnv";
ob_end_flush(); ?>