【发布时间】:2014-08-04 13:05:16
【问题描述】:
我需要将 html 解析的结果打印到 PHP 数组中。我被困在最后一部分。
library(XML)
url ='http://www.brainyquote.com/quotes/authors/j/john_kenneth_galbraith.html'
page <- htmlParse(url)
quote <- xpathSApply(page,
"//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]",
xmlValue)
quote = quote[nchar(quote) > 50] # this removes all none quotes based on string length
quote = quote[1:(length(quote)-2)] # this drops the last
out = paste(quote, collaspe= "', ") # how to get the ' at the front of the quote
write(out, "quote.txt")
最终代码的文本字符串末尾带有 --- quote here---',(apostrophe - comma)。我需要把'(撇号)放在开头,不知道该怎么做。我尝试将 r 用于 json,但不适用于我使用的简单 php 数组。这是这样的结构:
<?php
$quotes = array('quote goes here', 'quote goes here', 'final quote');
$rand = rand( 0, count($quotes)-1 );
echo $quotes[$rand];
?>
我并没有真正使用 php,但它只是在所有东西上运行,所以我做了这个随机报价生成器是简单的术语。我可以用 javascript 重写并使用 json 数组。但是我需要用javascript编写。
【问题讨论】: