简体   繁体   中英

PHP only match the words in array using regex

I am using str_replace to "clean" the words of the ones I do not desire, this way I can create a schedule for the teams. However, every time there is more words I need to eliminate, so I end up editing the file every day. I wanted to use regex, but I couldn't get only the team names. So I want to add the teams in an array and only get the team names thus cleaning the junk words.

so it would be something like:

Boston Celtics vs. Brooklyn Nets

That is if I can format it to include "vs." between both teams. Otherwise, it will be just

Boston Celtics Brooklyn Nets

here is the code I am using to remove the words.

<?php
$name1 = array("Spectrum Center","Charlotte, NC","Bankers Life Fieldhouse","Indianapolis, IN","Wells Fargo Center","Philadelphia, PA","Quicken Loans Arena","Cleveland, OH","bc","Chesapeake Energy Arena","Oklahoma City, OK","Toyota Center","Houston, TX","American Airlines Center","Dallas, TX","GAME PREVIEWBUY TICKETS","United Center","Chicago, IL","Target Center","Target Center","Staples Center","Los Angeles, CA","Minneapolis, MN","Amway Center","Orlando, FL","TD Garden","Boston, MA","Palace of Auburn Hills","Detroit, MI","Pepsi Center","Denver, CO","Vivint Smart Home Arena","Salt Lake City, UT","Talking Stick Resort Arena","Phoenix, AZ","Air Canada Centre","Toronto, ON","Barclays Center","Brooklyn, NY","FedExForum","Memphis, TN","ORACLE Arena", "Oakland, CA","Golden 1 Center","Sacramento, CA","Smoothie King Center","New Orleans, LA","BMO Harris Bradley Center","Milwaukee, WI","Philips Arena","Atlanta, GA","Madison Square Garden","New York, NY","AmericanAirlines Arena","Miami, FL","AT&T Center","San Antonio, TX","Verizon Center","Washington, DC","FOX SPORTS SOUTHEAST - CHARLOTTE","FS FLORIDA","WFNZ 610 AM","ESPN 580 / SALSA 98.1 FM (ESP)","WGN-TV","ROOT","WLS-AM 890 / WRTO 1200 (ESP)","790 AM","FSI","1070 THE FAN NORTH","CSN BAY AREA","830 WCCO","95.7 GAME SOUTHEAST","TSN2",'92.9 "THE GAME"',"TSN RADIO 1050 TORONTO ALTITUDE","CSNHD","KKSE 950AM","98.5 HUB FSSW","YES","103.3 ESPN","WFAN CSN-CA","CSN","KHTK 1140 1500 ","FOX SPORTS WISCONSIN","FSI","AM 620 WTMJ","1070 THE FAN","FS NORTH","CSN BAY AREA","830 WCCO","95.7 THE GAME","FOX SPORTS SOUTHEAST","TSN2","92.9 FM",'"THE GAME"',"TSN RADIO 1050 TORONTO","ALTITUDE","CSNHD","KKSE 950AM","98.5 THE SPORTS HUB","FSSW"," YES","103.3 ESPN","WFAN","-CA","CSN","TSN2","KHTK SPORTS 1140 AM","1500 AM","NBATV CANADA","FS OKLAHOMA HD","WWLS 98.1FM OKC / 930AM (ESP)","KZNS","PRIME TICKET","TCN-PHI","AM 570 LA SPORTS","97.5 THE FANATIC","FS DETROIT","MSG","105.1 WMGC-FM","ESPN NY 98.7","FSNOLA","WRNO 99.5FM","FSOHIO","WTAM 1100 / 100.7 WMMS / 87.7 FM (ESP)","FS-SUN","SPORTSNET ONE","SPORTSNET ONE","790 THE TICKET / S: WAQI 710 AM","SPORTSNET 590 THE FAN","WOAI 1200AM","WMFS","FSAZ","ARIZONA SPORTS 98.7FM / KSUN 1400AM (ES)","NWHD","RIP CITY RADIO 620","OR","SPE/SPECD","710 AM ESPN / 1330 AM ESPN DEPTES","WLS 890AM","KGWHD","-CHICAGO","CW35 HD","TSNN","TSN1/4","790 THE TICKET","/ S:","WRTO MIX 98.3 FM","TSN4/5","740 AM","100.7 WMMS / 87.7 FM (ESP)");

$name = $_POST["teamname"];
$name = str_replace($name1,"",$name);
$name = str_replace(",","",$name);
$name = str_replace("\n\n", '<br />', $name);
$name = str_replace("\r\n\r\n", '<br />', $name);

if(isset($_POST["teamname"])){
    echo $name;
    }
?>

is there a better way to acquire the result I am looking for.

edit:

I use the code in this answer to make the string of the schedule into an array. Hoever, i am having dificultes interacting with the regex loop i got from another answer (forgot the link) while they both work separately. i would need to first explode it into an array and ise that array to pass the regex in another loop. How can I use both loops?

this is what i have so far.

<?php
$search = array("Boston Celtics","Brooklyn Nets","New York Knicks","Philadelphia 76ers","Toronto Raptors","Golden State Warriors","LA Clippers","Los Angeles Lakers","Phoenix Suns","Sacramento Kings","Chicago Bulls","Cleveland Cavaliers","Detroit Pistons","Indiana Pacers","Milwaukee Bucks","Dallas Mavericks","Houston Rockets","Memphis Grizzlies","New Orleans Pelicans","San Antonio Spurs","Atlanta Hawks","Charlotte Hornets","Miami Heat","Orlando Magic","Washington Wizards","Denver Nuggets","Minnesota Timberwolves","Oklahoma City Thunder","Portland Trail Blazers","Utah Jazz","7:30 PM","7:00 PM");

$results= array(
"7:00 PM ET
CSN, FSI, 97.5 THE FANATIC, 1070 THE FAN
Indiana Pacers
Philadelphia 76ers",
       "7:30 PM ET
CSNHD, YES, 98.5 THE SPORTS HUB, WFAN
Brooklyn Nets
Boston Celtics",
       "7:30 PM ET
FS-SUN, FSOHIO, SN, 790 THE TICKET / S: WRTO MIX 98.3 FM, WTAM 1100 / 100.7 WMMS / 87.7 FM (ESP)
Cleveland Cavaliers
Miami Heat",
       "Excepteur sint occaecat cupidatat non proident"
    );
foreach ($results as $result) {
   $pattern = "/\b(" . implode('|', $search) . ")\b/i";
   $found   = preg_match_all($pattern, $result, $matches);
   $time = $matches[0][0];
   $team1 = $matches[0][1];
   $team2 = $matches[0][2];

   if ($found) {
       echo $time."&nbsp;&nbsp;&nbsp;".$team1." vs ".$team2."<br />";
   }
}

$t = "7:00 PM ET
CSN, FSI, 97.5 THE FANATIC, 1070 THE FAN
Indiana Pacers
Philadelphia 76ers
Wells Fargo Center
Philadelphia, PA
GAME PREVIEWBUY TICKETS
7:30 PM ET
CSNHD, YES, 98.5 THE SPORTS HUB, WFAN
Brooklyn Nets
Boston Celtics
TD Garden
Boston, MA
GAME PREVIEWBUY TICKETS
7:30 PM ET
FS-SUN, FSOHIO, SN, 790 THE TICKET / S: WRTO MIX 98.3 FM, WTAM 1100 / 100.7 WMMS / 87.7 FM (ESP)
Cleveland Cavaliers
Miami Heat
AmericanAirlines Arena
Miami, FL";
$pieces = explode("TICKETS", $t);

$teans = '';

foreach($pieces as $cat) {
    $cat = trim($cat);
    $teans .= "<category>" . $cat . "</category>\n";
    echo $cat."<br />";
}
?>

i did what i was looking for.

<?php
$search = array("Boston Celtics","Brooklyn Nets","New York Knicks","Philadelphia 76ers","Toronto Raptors","Golden State Warriors","LA Clippers","Los Angeles Lakers","Phoenix Suns","Sacramento Kings","Chicago Bulls","Cleveland Cavaliers","Detroit Pistons","Indiana Pacers","Milwaukee Bucks","Dallas Mavericks","Houston Rockets","Memphis Grizzlies","New Orleans Pelicans","San Antonio Spurs","Atlanta Hawks","Charlotte Hornets","Miami Heat","Orlando Magic","Washington Wizards","Denver Nuggets","Minnesota Timberwolves","Oklahoma City Thunder","Portland Trail Blazers","Utah Jazz","7:00 PM ","7:30 PM ","7:30 PM ","8:00 PM ","8:00 PM ","8:00 PM ","10:00 PM ","10:30 PM ","10:30 PM ");

$t = "7:00 PM ET
CSN, FSI, 97.5 THE FANATIC, 1070 THE FAN
Indiana Pacers
Philadelphia 76ers
Wells Fargo Center
Philadelphia, PA
GAME PREVIEWBUY TICKETS
7:30 PM ET
CSNHD, YES, 98.5 THE SPORTS HUB, WFAN
Brooklyn Nets
Boston Celtics
TD Garden
Boston, MA
GAME PREVIEWBUY TICKETS
7:30 PM ET
FS-SUN, FSOHIO, SN, 790 THE TICKET / S: WRTO MIX 98.3 FM, WTAM 1100 / 100.7 WMMS / 87.7 FM (ESP)
Cleveland Cavaliers
Miami Heat
AmericanAirlines Arena
Miami, FL
GAME PREVIEWBUY TICKETS
8:00 PM ET
CSN-CHICAGO, FS FLORIDA, WLS-AM 890 / WRTO 1200 (ESP), ESPN 580 / SALSA 98.1 FM (ESP)
Orlando Magic
Chicago Bulls
United Center
Chicago, IL
GAME PREVIEWBUY TICKETS
8:00 PM ET
FOX SPORTS WISCONSIN, FOX SPORTS SOUTHEAST - CHARLOTTE, AM 620 WTMJ, WFNZ 610 AM
Charlotte Hornets
Milwaukee Bucks
BMO Harris Bradley Center
Milwaukee, WI
GAME PREVIEWBUY TICKETS
8:00 PM ET
bc
TSNN, 105.1 WMGC-FM, 1500 AM
Washington Wizards
Detroit Pistons
Palace of Auburn Hills
Detroit, MI
GAME PREVIEWBUY TICKETS
10:00 PM ET
CSNNWHD, FSSW, RIP CITY RADIO 620, WOAI 1200AM
San Antonio Spurs
Portland Trail Blazers
Moda Center
Portland, OR
GAME PREVIEWBUY TICKETS
10:30 PM ET
bc
TSN1/4, AM 570 LA SPORTS, 790 AM
Houston Rockets
LA Clippers
Staples Center
Los Angeles, CA
GAME PREVIEWBUY TICKETS
10:30 PM ET
NBCS - BA, ROOT, SN, 95.7 THE GAME, KZNS
Utah Jazz
Golden State Warriors
ORACLE Arena
Oakland, CA";

$pieces = explode("TICKETS", $t);
foreach ($pieces as $result) {
   $pattern = "/\b(" . implode('|', $search) . ")\b/i";
   $found   = preg_match_all($pattern, $result, $matches);
   $time = $matches[0][0];
   $team1 = $matches[0][1];
   $team2 = $matches[0][2];

   if ($found) {
       echo $time."&nbsp;&nbsp;&nbsp;".$team1." vs ".$team2."<br />";
   }
}
?>

DEMO http://rextester.com/THKE48416

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM