1

I make my daily NBA schedules using the nba.com site here, but they changed the layout of it now it contains more data. Using PHP, I was able to turn

this:

5:00 PM ET
LA Clippers
Charlotte Hornets
Spectrum Center
Charlotte, NC
GAME PREVIEWBUY TICKETS

into this:

5:00 PM ET
LA Clippers
Charlotte Hornets

However, I am having difficulties turning that into this:

5:00 PM ET LA Clippers @ Charlotte Hornets

I am open to suggestion even other languages like javascript/jquery that I could implement into a userscript if not PHP.

any suggestions?

code I'm currently using to clean the words i don't want.

<?php
$name = $_POST["teamname"];
$words = 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","Vivint Smart Home Arena","
Salt Lake City, UT");
$name = str_replace($words,"",$name);

if(isset($_POST["teamname"])){
    echo 'output:<br /><br /><textarea name="teamname" cols="80" rows="20" onclick="this.focus();this.select()">'.$name.'</textarea>';
    }
?>

edit: added my code.

3
  • 1
    Paste in the code you've got already so SO users can help you modify it. Commented Feb 10, 2017 at 2:52
  • 1
    change your last line break to an @. relly need to see your current code on this one Commented Feb 10, 2017 at 2:55
  • added the PHP code i am using. Commented Feb 10, 2017 at 2:59

1 Answer 1

1

At javascript you can call .split("\n") on string, then use Array.prototype.splice() to add one or more elements to array, then call .join("") on array to return string.

let data = `5:00 PM ET
LA Clippers
Charlotte Hornets
Spectrum Center
Charlotte, NC
GAME PREVIEWBUY TICKETS`;

let arr = data.split("\n").slice(0, 3);
arr.splice(-1, 0, " @ ");
arr.splice(1, 0, " ");

console.log(arr.join(""));

Given string at jsfiddle, with "bc" omitted at line 26, you can use a do..while loop, increment a variable by 6 to skip three lines before next date line.

let data = `5:00 PM ET
LA Clippers
Charlotte Hornets
Spectrum Center
Charlotte, NC
GAME PREVIEWBUY TICKETS
7:00 PM ET
Milwaukee Bucks
Indiana Pacers
Bankers Life Fieldhouse
Indianapolis, IN
GAME PREVIEWBUY TICKETS
7:30 PM ET
Miami Heat
Philadelphia 76ers
Wells Fargo Center
Philadelphia, PA
GAME PREVIEWBUY TICKETS
7:30 PM ET
Denver Nuggets
Cleveland Cavaliers
Quicken Loans Arena
Cleveland, OH
GAME PREVIEWBUY TICKETS
8:30 PM ET
Golden State Warriors
Oklahoma City Thunder
Chesapeake Energy Arena
Oklahoma City, OK
GAME PREVIEWBUY TICKETS
9:00 PM ET
Phoenix Suns
Houston Rockets
Toyota Center
Houston, TX
GAME PREVIEWBUY TICKETS
9:00 PM ET
Orlando Magic
Dallas Mavericks
American Airlines Center
Dallas, TX
GAME PREVIEWBUY TICKETS
10:00 PM ET
Boston Celtics
Utah Jazz
Vivint Smart Home Arena
Salt Lake City, UT`;

let [arr, n, next, res] = [data.split("\n"), 0, 6, Array()];

do {
  let curr = arr.slice(n, n + 3);
  curr.splice(-1, 0, " @ ");
  curr.splice(1, 0, " ");
  res.push(curr.join(""));
  n += next;
} while (n + next < arr.length);

console.log(res);

var text = "";
var i;
for (i = 0; i < res.length; i++) {
    text += res[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
<div id="demo"></div>

Sign up to request clarification or add additional context in comments.

6 Comments

I tried this example, but when I add more than one team it only reads the first, would I have to add a for loop?
Yes. You can request N teams using Promise.all(), fetch() or XMLHttpRequest(), then iterate response array to perform same action on each response.
i tried a for loop, but i was only able to get the array from the loop if i commented arr.splice(-1, 0, " @ "); arr.splice(1, 0, " "); otherwise i just got the first entry.
Note, the approach parses string at javascript, not php. Used the string at original Question. Are newline characters included in returned string? Can you reproduce getting the string described at Question at stacksnippets or jsfiddle jsfiddle.net?
here you can see the part I commented, if I leave the slice, then it only shows one event. jsfiddle.net/8d458kmg/3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.