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
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
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
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.