gpt4 book ai didi

javascript - PHP - 格式化字符串

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:39 24 4
gpt4 key购买 nike

我使用 nba.com 网站 here 制定每日 NBA 日程表,但他们改变了它的布局,现在它包含更多数据。使用 PHP,我能够转动

这个:

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

进入此:

5:00 PM ET
LA Clippers
Charlotte Hornets

但是,我很难将其变成这样:

5:00 PM ET LA Clippers @ Charlotte Hornets

我愿意接受建议,即使不是 PHP,我也可以将其他语言(例如 javascript/jquery)实现到用户脚本中。

有什么建议吗?

我目前正在使用的代码来清理我不想要的单词。

<?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>';
}
?>

编辑:添加我的代码。

最佳答案

javascript中,您可以对字符串调用.split("\n"),然后使用Array.prototype.splice()向数组添加一个或多个元素,然后对数组调用 .join("") 以返回字符串。

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(""));

给定 jsfiddle 的字符串,在第 26 行省略 "bc",您可以使用 do..while 循环,递增变量 6 在下一个日期行之前跳过三行。

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>

关于javascript - PHP - 格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com