【发布时间】:2012-08-07 23:25:40
【问题描述】:
这是我的代码..
<?php
// first get all the html contents
$page = file_get_contents('http://www.planetled.com.au/blog/what-is-a-led-spotlight/');
//next explode it
$words = explode(" ", $page);
// then display the 21st word or any word you like
echo $words[22]; // 21st word
?>
接下来我想要找到第 21 个人类可读的单词。我正在考虑使用preg_match 来查找单词,然后使用str_replace,但我缺少的是我将在 preg_match 中使用什么模式?谢谢。
【问题讨论】:
-
第 21 个单词在索引 20,而不是 22
-
那么您能确定正在加载的页面实际上只是一个单词列表,还是其中有其他内容?如果是这样,您的
explode将受到威胁。此外,数组的第 21 个键是$arr[20],而不是 21(因为 0 是第一个)。 -
我不明白为什么你需要正则表达式来查找单词,当你已经分解了单词数组并且只需要在 index = 20 处访问单词
-
如果要过滤掉html标签,拜托拜托,拜托forget about regex,只解析文档!
标签: php preg-match str-replace