timmah1 Posted January 28, 2009 Share Posted January 28, 2009 I'm trying to show the Minus whatever if a team was found, meaning, I only want to show Minus 5 of this sentence New Orleans Saints Minus 5 Total 45, if the team matches. So I did this <?php if (preg_match("/\b$a[team1]\b/i", "$a[overunder]")) { echo "A match was found."; } else { echo "A match was not found."; } ?> Now, how do I get it to only show Minus 5 of that sentence, and not the whole sentence? The number will always be different, but the word Minus will always be there Thanks in advance Quote Link to comment Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 Try that and see if you get the required result; <?php $pattern = '/Minus [0-9]+/'; $ubject = $a[overunder]; preg_match($pattern, $subject, $matches); print_r($matches); Quote Link to comment Share on other sites More sharing options...
corbin Posted January 28, 2009 Share Posted January 28, 2009 First off, you have a few iffy-basics: Should be: if (preg_match("/\b{$a['team1']}\b/i", $a['overunder'])) { (The {} are mainly personal preference in this situation, but in some contexts they are required.) Will $a['overunder'] always contain "<team> Minus X Total Y"? If so: if (preg_match("/minus ([\d]+) total ([\d]+)/i", $a['overunder'], $m)) { print_r($m); } (That assumes that the full string will match the format from earlier. In other words, it assumes only 1 team is in the string at a time.) Quote Link to comment Share on other sites More sharing options...
timmah1 Posted January 28, 2009 Author Share Posted January 28, 2009 Perfect! one more question, how do I format that? I don't want it to show Array ( [0] => Minus 5 ) Quote Link to comment Share on other sites More sharing options...
timmah1 Posted January 28, 2009 Author Share Posted January 28, 2009 the overunder will always contain a team name and always have the word Minus in it Quote Link to comment Share on other sites More sharing options...
bluesoul Posted January 28, 2009 Share Posted January 28, 2009 echo $m[0]; Quote Link to comment Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 instead of; print_r($whatever_variable); use echo $whatever_variable[0]; Quote Link to comment Share on other sites More sharing options...
timmah1 Posted January 28, 2009 Author Share Posted January 28, 2009 One more question, this statement $pattern = '/Minus [0-9]+/'; only goes up to the number 9 correct? Sometimes it might be as high as 60, when I change the 9 to 60, nothing shows Quote Link to comment Share on other sites More sharing options...
corbin Posted January 28, 2009 Share Posted January 28, 2009 instead of; print_r($whatever_variable); use echo $whatever_variable[0]; The first value always contains the entire string matched. He would want to use 1 and 2. Also, if you'll read my post, timmah, you'll see that I gave a pattern that will extract minus and total, and it will handle any number of digits. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted January 28, 2009 Author Share Posted January 28, 2009 I used the code you supplied corbin, but I get no results now <?php if (preg_match("/\b{$a['team1']}\b/i", $a['overunder'])) { if (preg_match("/minus ([\d]+) total ([\d]+)/i", $a['overunder'], $m)) { print_r($m); } } ?> Did I implement that wrong? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 @corbin, I was refering to the code that I'd written as he only wanted the minus part Quote Link to comment Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 Also, that pattern should (and doesn from my test) match as many integers are necessary Quote Link to comment Share on other sites More sharing options...
timmah1 Posted January 28, 2009 Author Share Posted January 28, 2009 Thank you for your help gevans and corbin Your code gevans does exactly what I need it to do, I just want to make sure that if the minus is 22, it will still show Thanks again Quote Link to comment Share on other sites More sharing options...
gevans Posted January 28, 2009 Share Posted January 28, 2009 It shouldn't need any changes, but to force it to work you can just add a space to the end of the pattern; $pattern = '/Minus [0-9]+ /'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.