How to find specific number of characters after something ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

How to find specific number of characters after something ?

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: How to find specific number of characters after something ?

Post by Mark Baker »

Look at the PREG_OFFSET_CAPTURE flag for preg_match(). That returns the position within the string of each match found
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How to find specific number of characters after something ?

Post 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.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post 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: "
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How to find specific number of characters after something ?

Post by John Cartwright »

strpos() + strlen(), or preg_match(,,,PREG_OFFSET_CAPTURE) (as Mark Baker pointed out)
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How to find specific number of characters after something ?

Post 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: ".
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How to find specific number of characters after something ?

Post by mattpointblank »

That's not the correct usage, it's more like:

Code: Select all

 
$code = "i: 123456";
$dynamicNumber = substr($code, 3);
 
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post 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 ?
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post 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?
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post by lovelf »

I am almost there, I will post the solution in case someone finds it useful in the future :wink:
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to find specific number of characters after something ?

Post 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(); ?>
Post Reply