新手求助获取字符串

jellday 2008-04-13 11:10:45
1、远程获取文件,并读入变量
代码如下:
function getW($wLink){

if ($fp = fopen($wLink, 'r')) {
$content = '';

while ($line = fread($fp, 1024)) {
$content .= $line;
}
}
return $content;
}

远程文件源代码包含如下代码“
<yw:cdtn text="Fair" code="34" temp="7" date="Thu, 10 Apr 2008 9:50 am BST" />
...
<yw:f day="Thu" date="10 Apr 2008" low="1" high="11" text="Light Rain" code="11" />
<yw:f day="Fri" date="11 Apr 2008" low="1" high="10" text="Light Rain" code="11" />

其中"yw:cdtn", "yw:f"关键字为固定不变的, "text", "day", "date", "low", "high"等关键字变化,且yw:f下的day和date与当前相差一天即明天和后天。
yw:cdtn只有一个,yw:f有两个。
2、想获取"yw:cdtn text"的值"Fair",以及yw:f下面的"day", "low","high","text"的值,并分别存入各个变量。应该怎么办?
...全文
43 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
张吉Jerry 2008-04-13
  • 打赏
  • 举报
回复
preg_match("/<yw:cdtn.*?\/>/",$content,$matches);
preg_match_all("/(text|code|temp|date)=\"([^\"]+)\"/",$matches[0],$matches,PREG_SET_ORDER);
foreach($matches as $value) $cdtn[$value[1]]=$value[2];

preg_match_all("/<yw:f.*?\/>/",$content,$matches);
foreach($matches[0] as $key=>$value){
preg_match_all("/(day|date|low|high|text|code)=\"([^\"]+)\"/",$value,$matches_,PREG_SET_ORDER);
foreach($matches_ as $value_) $f[$key][$value_[1]]=$value_[2];
}

var_dump($cdtn);
var_dump($f);

输出结果如下:
array(4) {//$cdtn
["text"]=>
string(4) "Fair"
["code"]=>
string(2) "34"
["temp"]=>
string(1) "7"
["date"]=>
string(28) "Thu, 10 Apr 2008 9:50 am BST"
}
array(2) {//$f
[0]=>
array(6) {
["day"]=>
string(3) "Thu"
["date"]=>
string(11) "10 Apr 2008"
["low"]=>
string(1) "1"
["high"]=>
string(2) "11"
["text"]=>
string(10) "Light Rain"
["code"]=>
string(2) "11"
}
[1]=>
array(6) {
["day"]=>
string(3) "Fri"
["date"]=>
string(11) "11 Apr 2008"
["low"]=>
string(1) "1"
["high"]=>
string(2) "10"
["text"]=>
string(10) "Light Rain"
["code"]=>
string(2) "11"
}
}
jellday 2008-04-13
  • 打赏
  • 举报
回复
"text", "day", "date", "low", "high"等关键字变化
说错了,应该是"text", "day", "date", "low", "high"等关键字的值变化。即远程文件引号内的值是可变的。

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧