Page 1 of 1

sstr

Posted: Wed Nov 17, 2004 3:39 pm
by SidewinderX
hello i need a little help with sscanf

my current code i have is

Code: Select all

<?php
$str = "Rank:
5-Star General ( 18 ) ";

sscanf($str, 'Rank: %s' , $rank);
 
echo "$rank";
?>
Which outputs '5-Star'...i dont want that..i want it to say 5-Star General ( 18 )

and the str format must remain exactley as is

Posted: Wed Nov 17, 2004 4:04 pm
by MarK (CZ)
It depends on what other values you're going to use this. In this one,

Code: Select all

$rank = SubStr($str, 5);
would be enough.

Posted: Wed Nov 17, 2004 4:06 pm
by rehfeld

Code: Select all

<?php
$str = "Rank:
5-Star General ( 18 ) ";

sscanf($str, 'Rank: %s %s %s %s %s' , $star, $general, $p1, $num, $p2);
 
echo "$star $general $p1 $num $p2";

?>
but this might not be the best solution to parsing out the word "Rank: "

how much(and how) will the input $str vary from the format you posted?

theres likely much simpler solutions than this....

Posted: Wed Nov 17, 2004 4:49 pm
by SidewinderX
well if you go here: http://24.190.94.106/phpform.php and click "Get Source" that will display basically what the entire string well be... what this script is doing is getting info from a place where my player stats are kept on the internet http://24.190.94.106/phpform.php~ for the source...

Esentially what im going to do is when i combined with my new code

Code: Select all

<?php
$str = "Rank:
5-Star General ( 18 ) ";
sscanf($str, 'Rank: %s' , $rank); 
echo "$rank";?>
?>
with the other one is get rid of the current $str and replace it with the $stats variable from the larger script.

Now what im trying to do is store each stat (the numbers) as a variable (which do not remain constant) so i will be able to call them out in a later in a different place...the stats do not remain constant but the headings do
(ie Rank: PCID: Base Targets Destroyed:)...

once i get past the Rank (which as 18 different variations) i plan on just doing this

Code: Select all

<?php

$str = "Base Targets Destroyed 
835 
Flags Captured 
1271 
Flags Saved 
260 
";
sscanf($str, 'Base Targets Destroyed %d Flags Captured %d Flags Saved %d' , $stat_base_targets_destroyed,$stat_flags_captured,$stat_flags_saved);
echo "$stat_base_targets_destroyed $stat_flags_captured $stat_flags_saved";

?>
which parses out the headings (which i want) and stores the stats as a variable (which i want) it is tedious but does what i want...the stumbeling block is the rank and i dont think the %s %s %s %s idea would work because it does vary from Private to 5-Star General and i dont think SubStr($str, 5); will work either due to the differing ranks

Posted: Wed Nov 17, 2004 5:53 pm
by MarK (CZ)
Maybe you could split the text into array using

Code: Select all

$statsArr = Explode("\r\n", $str);
I guess that you will be able to handle the data easily then.

Posted: Wed Nov 17, 2004 6:42 pm
by timvw
in pseudo i would

remove all \r\n
split on <tr>
foreach line
preg_match on <td ..>(.*?)</td><td ..>(.*?)<td>