Jump to content

Preg_Match help


timmah1

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/142842-preg_match-help/
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/142842-preg_match-help/#findComment-748785
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/142842-preg_match-help/#findComment-748801
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.