Array question...

I am doing a search replace here is the code…

 $find = array(
      "TBD",
      "ARI",
      "ATL",
      'BAL',
      'BUF',
      'CAR',
      'CHI',
      'CIN',
      'CLE',
      'DAL',
      'DEN',
      'DET',
      'GB',
      'HOU',
      'IND',
      'JAC',
      'KC',
      'MIA',
      'MIN',
      'NE',
      'NO',
      'NYJ',
      'NYG',
      'OAK',
      'PHI',
      'PIT',
      'STL',
      'SD',
      'SF',
      'SEA',
      'TB',
      'TEN',
      'WAS',
      'RIC',
      'IRV'
      
  );
  
  $replace = array(
    "<img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/tbd.png'>&nbsp;&nbsp; TBD</a>",
    "<a href='http://www.azcardinals.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/cards.png'>&nbsp;&nbsp; Cardinals</a>", 
    "<a href='http://www.atlantafalcons.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/falcons.png' >&nbsp;&nbsp; Falcons</a>", 
    "<a href='http://www.baltimoreravens.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/ravens.png' >&nbsp;&nbsp; Ravens</a>", 
    "<a href='http://www.buffalobills.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/bills.png' >&nbsp;&nbsp; Bills</a>", 
    "<a href='http://www.panthers.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/panthers.png' >&nbsp;&nbsp; Panthers</a>", 
  	"<a href='http://www.chicagobears.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/bears.png' >&nbsp;&nbsp; Bears</a>", 
  	"<a href='http://www.bengals.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/bengals.png' >&nbsp;&nbsp; Bengals</a>", 
  	"<a href='http://www.clevelandbrowns.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/browns.png' >&nbsp;&nbsp; Browns</a>", 
  	"<a href='http://www.dallascowboys.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/cowboys.png' >&nbsp;&nbsp; Cowboys</a>", 
  	"<a href='http://www.denverbroncos.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/broncos.png' >&nbsp;&nbsp; Broncos</a>", 
  	"<a href='http://www.detroitlions.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/lions.png' >&nbsp;&nbsp; Lions</a>", 
  	"<a href='http://www.packers.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/packers.png' >&nbsp;&nbsp; Packers</a>", 
  	"<a href='http://www.houstontexans.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/texans.png' >&nbsp;&nbsp; Texans</a>", 
  	"<a href='http://www.colts.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/colts.png' >&nbsp;&nbsp; Colts</a>", 
  	"<a href='http://www.jaguars.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/jags.png' >&nbsp;&nbsp; Jaguars</a>", 
  	"<a href='http://www.chiefs.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/chiefs.png' >&nbsp;&nbsp; Chiefs</a>", 
  	"<a href='http://www.miamidolphins.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/dolphins.png' >&nbsp;&nbsp; Dolphins</a>", 
  	"<a href='http://www.vikings.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/viks.png' >&nbsp;&nbsp; Vikings</a>", 
  	"<a href='http://www.patriots.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/pats.png' >&nbsp;&nbsp; Patriots</a>", 
  	"<a href='http://www.neworleanssaints.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/saints.png' >&nbsp;&nbsp; Saints</a>",
  	"<a href='http://www.newyorkjets.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/jets.png' >&nbsp;&nbsp; Jets</a>",
  	"<a href='http://www.giants.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/giants.png' >&nbsp;&nbsp; Giants</a>", 
  	"<a href='http://www.raiders.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/raiders.png' >&nbsp;&nbsp; Raiders</a>", 
  	"<a href='http://www.philadelphiaeagles.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/eagles.png' >&nbsp;&nbsp; Eagles</a>", 
  	"<a href='http://www.steelers.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/steelers.png' >&nbsp;&nbsp; Steelers</a>",
   	"<a href='http://www.stlouisrams.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/rams.png' >&nbsp;&nbsp; Rams</a>", 
  	"<a href='http://www.chargers.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/chargers.png' >&nbsp;&nbsp; Chargers</a>", 
  	"<a href='http://www.49ers.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/49ers.png' >&nbsp;&nbsp; 49ers</a>", 
  	"<a href='http://www.seahawks.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/seahawks.png' >&nbsp;&nbsp; Seahawks</a>", 
  	"<a href='http://www.buccaneers.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/bucs.png' >&nbsp;&nbsp; Buccaneers</a>", 
  	"<a href='http://www.titansonline.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/titans.png' >&nbsp;&nbsp; Titans</a>", 
  	"<a href='http://www.redskins.com/' target='_blank'><img src='". JUri::base() ."/modules/mod_scores/assets/img/teams/redskins.png' >&nbsp;&nbsp; Redskins</a>", 
  	"Team Rice",
  	"Team Irving"
    );
$visitingTeam = str_replace($find, $replace, $game['v']);

It was working perfectly then a curve ball…

TB and TBD are so close that when it’s doing the search replace it’s combining both strings of the array into one string…

Basically the output is…

img file Buccaneers score

With the TBD and TB it’s doing this…

image file[TBD] image file[TB] BuccaneersD score

Any suggestions how to fix this? I’ve tried preg_replace but it does the same thing…

Thank yoU!

What exactly are you trying to accomplish overall? It looks like you are not going about the right way.

I am reading an xml file from the NFL and parsing that into result that I have in a module to show the scores etc on my site… I’ve attached the entire file [yes I know it’s messy but I’m still working on it] :slight_smile:

It’ll be easier then pasting snippets of code… that way it’ll be easier to see what I’m doing with it.

Thank you!


default.zip (3.17 KB)

I believe the only real question I had was I have one array item TB and a second that is close TBD

It’s picking up both when it shows the results…

The question was how can I do this and have it pick up one or the other and not both? For right now I took the TB out … but during a regular season i would need it back…

I can think of a long way around the problem but I’d rather learn where my mistakes are and have it shorter…

Thanks!

OH and the updated version I have taken out the weather element out…

Well, there is a lot of odd things that can happen when strings are involved. Sometimes it is the way that they
were saved in the first place. In your array, you use both quotes and double-quotes to save the strings. This
is not the same. Also, how you compare two strings can be dozens of different ways depending on what you
really want to know. Normally, to compare two strings to be “IDENTICAL” you use the ($a===$b) format. If
you do the standard == instead, it first alters the types to be fit into a standard string format depending on
how the variable was created. (Different ways for quotes and double-quotes!) Then, after it converts them to
the same standard they are compared. But, also, you can run a compare that checks only for the length of the
smaller of the two. This adds further trouble to this “simple” compare…

I would first alter your array creation to be all double-quotes. If that solves it, all is good. If not, read this page:
http://php.net/manual/en/language.operators.comparison.php
It might explain how to handle your compare.

The next test would be to just try using the IDENTICAL option which is the === in your IF clause…
Let us know…

You need something like this so you don’t get half truths:

[php]<?php

$output = [
“cake”,
“pancake”,
“cakes”,
];

$subject = “cake”;

foreach ( $output as $out ) {
echo “

Checking $out

”;
if ( preg_match( “/$out/”, $subject)) {
echo “

Found $subject: $out

”;
} else {
echo “

$out does not match $subject

”;
}
}
[/php]

I see and understand. I know I’ve looked at it and for some reason it didn’t occur to me I was in fact using both single and double quotations.

I appreciate your replay and I will certainly make those changes and read the link anyway, it can’t hurt :wink:

Thank you very much!!

I really Thank all you guys for the input!! I’m gonna go ahead and mark this as solved.

I see I need to continue working on some things here. JimL helped me get this entire thing off the ground last year and I’ve been tweaking and adding to it here and there. Trust me it was a lot longer 2 months ago but I have cut it way down…

Thanks again!!

Actually, preg_replace should work. How were you attempting to use it?

I thought so too… I’ll just give you the snippet of what and how it’s being used…

[php]
$find = array(
“ARI”,
“ATL”,
“BAL”,
“BUF”,
“CAR”,
“CHI”,
“CIN”,
“CLE”,
“DAL”,
“DEN”,
“DET”,
“GB”,
“HOU”,
“IND”,
“JAC”,
“KC”,
“MIA”,
“MIN”,
“NE”,
“NO”,
“NYJ”,
“NYG”,
“OAK”,
“PHI”,
“PIT”,
“STL”,
“SD”,
“SF”,
“SEA”,
//“TB”,
“TBD”,
“TEN”,
“WAS”,
“IRV”,
“RIC”

);

$replace = array(
"   Cardinals",
"   Falcons",
"   Ravens",
"   Bills",
"   Panthers",
"   Bears",
"   Bengals",
"   Browns",
"   Cowboys",
"   Broncos",
"   Lions",
"   Packers",
"   Texans",
"   Colts",
"   Jaguars",
"   Chiefs",
"   Dolphins",
"   Vikings",
"   Patriots",
"   Saints",
"   Jets",
"   Giants",
"   Raiders",
"   Eagles",
"   Steelers",
"   Rams",
"   Chargers",
"   49ers",
"   Seahawks",
//"   Buccaneers",
"   TBD",
"   Titans",
"   Redskins",
“Team Irvin”,
“Team Rice”
);

$homeTeam = str_replace($find, $replace, $game[‘h’]);
$visitingTeam = str_replace($find, $replace, $game[‘v’]);

<?= $homeTeam ?> <?= $game['hs'] ?>
<?= $visitingTeam ?> <?= $game['vs'] ?>

[/php]

I have 2 arrays filled with team ‘info’… the first array is what is coming from an xml file…
The second is what I’m replacing it with.

The last 2 lines are the output…

I was str_replace because I thought it was more strict in what it was looking for.

First,

$homeTeam = str_replace($find, $replace, $game[‘h’]);
$visitingTeam = str_replace($find, $replace, $game[‘v’]);

Don’t do this,
[php] <?= $homeTeam ?> <?= $game['hs'] ?>

<?= $visitingTeam ?> <?= $game['vs'] ?> [/php]

You should never break into and out of php when php follows itself.

[php]

<?php echo "

{$homeTeam} {$game['hs']}

"; echo "

{$visitingTeam} {$game['vs']}

"; ?> [/php]

Try this and see what happens:

[php] $homeTeam = preg_replace("/$find/", $replace, $game[‘h’]);
$visitingTeam = preg_replace("/$find/", $replace, $game[‘v’]);[/php]

preg_replace is returning nothing…

So instead of this
Team Logo Name Score

I’m getting this:
Score

Not sure why it’s doing that… it should have worked… hummmmmmm

I didn’t download the file, so I don’t know what is in the $game[‘h’] or $game[‘v’] strings.

They are the strings from the xml and they come from this:
http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml

I would go another way:

[php]try {

$xml = simplexml_load_file ( "http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml" );
for($i = 0; $i < count ( $xml->gms->g ); $i ++) {
	echo "<p>{$xml->gms->g[ $i ]['htn']}</p>";	}
echo "<hr>";

} catch ( Exception $e ) {
echo $e->getMessage ();
}[/php]

Outputs:

Houston Texans

Cincinnati Bengals

New England Patriots

Denver Broncos

Carolina Panthers

Arizona Cardinals

Minnesota Vikings

Washington Redskins

Team Michael Irvin

Thank you very much! I will be working on it tomorrow as I’m not at home until then.

It looks like it’ll catch every instance and return it… that’s a very interesting way to handle it! :slight_smile:

Thank you, will let you know!

I got it working like this:

[php]
$xml = simplexml_load_file ( “http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml” );
for ($i = 0; $i < count ( $xml->gms->g ); $i ++) {
echo “

{$xml->gms->g[ $i ][‘htn’]}  {$xml->gms->g[ $i ][‘hs’]}

”;

echo “

{$xml->gms->g[ $i ][‘vtn’]}  {$xml->gms->g[ $i ][‘vs’]}

”;
echo “
”;
}

[/php]

When I put this part in:

[php]
catch ( Exception $e ) {
echo $e->getMessage ();
}
[/php]

It quits working… That is where I’m sure it’s catching the TBD but again when I use it it quits working…

Suggestions?

And
Thank yoU!

That’s not where it’s catching TBD and has nothing to do with it. It is for exception handling and is probably not working, because you chopped portions of it off.

It works and I continue to play around with it… problem is I’m able to get info from 2 different xml files that they post… one being regular season and one post season… the following allows me to do that :slight_smile:

Not only that it shortens the code a HUGE amount… and runs much quicker! :slight_smile:

[php]
$home = $xml->gms->g[ $i ][‘hnn’];
$visit = $xml->gms->g[ $i ][‘vnn’];
$hometeam = ucfirst($home);
$vistteam = ucfirst($visit);
$time = $xml->gms->g[ $i ][‘q’];

if ($xml->gms->g[ $i ]['h'] == "TBD" and $xml->gms->g[ $i ]['v'] == "TBD") {
  echo "<p>{$xml->gms->g[ $i ]['gt']}</p>";
  echo "<p>TBD&nbsp;&nbsp;{$xml->gms->g[ $i ]['hs']}</p>";	
  echo "<p>" . $vistteam . "&nbsp;&nbsp;{$xml->gms->g[ $i ]['vs']}</p>";
  echo "<hr>";
}
elseif ($xml->gms->g[ $i ]['h']== "TBD" ){
  echo "<p>{$xml->gms->g[ $i ]['gt']}</p>";
  echo "<p>TBD&nbsp;&nbsp;{$xml->gms->g[ $i ]['hs']}</p>";	
  echo "<p>TBD&nbsp;&nbsp;{$xml->gms->g[ $i ]['vs']}</p>";
  echo "<hr>";
}  
else {
  echo "<p>{$xml->gms->g[ $i ]['gt']}</p>";
  echo "<p>" . $hometeam . " &nbsp;&nbsp;{$xml->gms->g[ $i ]['hs']}</p>";	
  echo "<p>" . $vistteam . " &nbsp;&nbsp;{$xml->gms->g[ $i ]['vs']}</p>";
   if ($time == "F") {
   	  echo "<font color=red> Final </font>";
   	  echo "<hr>";
   }
   else if ($time == "P") {
   	echo "<font color=red> {$xml->gms->g[ $i ]['d']}</font>";
    echo "<hr>";
   }
   else {
   	 echo "<font color=red> {$xml->gms->g[ $i ]['q']}</font>";
    echo "<hr>";
   }

  
}}

?>
[/php]

Still having a bit of problem with the TBD thing… it reads some but not all … not sure why… LOL progress!

Thank yoU!

Sponsor our Newsletter | Privacy Policy | Terms of Service