sstr

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
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

sstr

Post 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
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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....
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
Last edited by SidewinderX on Thu Nov 18, 2004 3:10 pm, edited 1 time in total.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

in pseudo i would

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