我有一个像这样的xml文件:
<volume name="Early">
<book name="School Years">
<chapter number="1">
<line number="1">Here's the first line with Chicago in it.</line>
<line number="2">Here's a line that talks about Atlanta</line>
<line number="3">Here's a line that says chicagogo </line>
</chapter>
</book>
</volume>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用PHP进行简单的关键字搜索,找到该单词并显示它所在的行.我有这个工作
$xml = simplexml_load_file($data);
$keyword = $_GET['keyword'];
$kw=$xml->xpath("//line[contains(text(),'$keyword')]");
...snip...
echo $kw[0]." is the first returned item";
Run Code Online (Sandbox Code Playgroud)
但是,使用此技术,用户必须搜索"芝加哥"而不是"芝加哥",否则搜索将不返回任何内容.
我知道我需要使用翻译功能,但我的所有试验和错误都是徒劳的.
我试过了:
$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$lower = "abcdefghijklmnopqrstuvwxyz";
$kw = $xml->xpath("line[contains(text(),'translate('$keyword','$upper','$lower'))]");
Run Code Online (Sandbox Code Playgroud)
但似乎没什么用.有小费吗?
使用以下生成的数组示例结构,如何循环并从每个对象中提取属性名称及其关联值?
[{"bg_2":"0.50"},{"bg_7":"0.10"},{"bg_12":"0.20"}]
Run Code Online (Sandbox Code Playgroud)
对象的数量可能会更改,并且属性名称将不一致.
我有一个循环遍历数组的foreach循环(simpleXML节点).该数组中可以包含0到几百个项目.我想找到一种方法来显示前10个结果,然后有一个链接显示下10个,依此类推.
例如,我目前有:
$i=0;
$limit=10;
foreach ($nodes as $node){
echo "here is the output: ".$node."<br>\n";
if (++$i >=$limit) break;
}
Run Code Online (Sandbox Code Playgroud)
很明显,无论$ nodes数组中有多少项,它只显示前10个.但我想我读过每次循环时foreach循环重置计数器 - 所以如果我想要一个链接说:next 10 itmes- 我我不确定如何告诉循环开始索引= 10.
我甚至在这里咆哮着正确的树吗?
我搜索并找到了一些相似但不完全的答案。
我有一个数组SongList(为简洁起见,显示 2 个项目...) - 第一对是键,第二对是一些 JSON。
SongList={
song_1:{title:"title of first song",artist:"the artist",mp3:"http://mysite/song1.mp3"},
song_2:{title:"title of second song",artist:"the artist",mp3:"http://mysite/song2mp3"}
...
};
Run Code Online (Sandbox Code Playgroud)
我希望能够检索给定值中的键(song_1或song_2)title。
我将循环遍历一个临时i项目数组,该数组中的每个项目都会有一个匹配项SongList,并且我会将键 ( song_1, song_2) 保存在最终数组中。
我正在使用名为 images.example.com 的 Amazon S3 存储桶,该存储桶使用如下 URL 成功通过 Cloudflare CDN 提供内容:
https://images.example.com/myfile.jpg
Run Code Online (Sandbox Code Playgroud)
我想防止热链接到图像和其他内容,购买限制仅访问引用域:example.com 以及可能用作开发服务器的另一个域。
我尝试过一种存储桶策略,它既允许来自特定域,又拒绝来自任何非特定域的域:
{
"Version": "2012-10-17",
"Id": "http referer policy example",
"Statement": [
{
"Sid": "Allow get requests referred by www.example.com",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::images.example.com/*",
"Condition": {
"StringLike": {
"aws:Referer": [
"http://www.example.com/*",
"http://example.com/*"
]
}
}
},
{
"Sid": "Explicit deny to ensure requests are allowed only from specific referer.",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::images.example.com/*",
"Condition": {
"StringNotLike": {
"aws:Referer": [ …Run Code Online (Sandbox Code Playgroud) 我从多维数组中的给定值获取键.它工作得很好,除了我似乎无法从OUTSIDE访问我用于获取密钥的嵌套foreach循环中的变量.
所以我的foreach循环是:($ name_books是包含3个较小数组的多维数组)
foreach($name_books as $test) {
foreach ($test as $key => $value) {
$book_code = array_search($row['name'],$test);
echo $book_code; //just to see if it works, which it does
break;
}
}
//But then if I go outside of the loop..
echo $book_code." is the book code"; // <--DOES NOT WORK
Run Code Online (Sandbox Code Playgroud)
所以我知道我正在处理变量范围问题,我已经尝试在foreach循环中声明全局变量,但没有任何效果.
我确信有一些荒谬的简单,我错过了!
编辑:
urg ..我退后一步,意识到其他的事情,所有这一切都发生在一个while循环中(从数据库中获取东西)
所以代码更像是:
while($row=mysql_fetch_assoc($result)) {
...original foreach loop from above
}
Run Code Online (Sandbox Code Playgroud)
抱歉不包括这个,我专注于这个小块而忘了备份,看看它适合哪里.
在我的页面上,当用户单击"删除"按钮时,将删除与变量匹配的选择列表中的项目并更新dom.除非有问题的变量中有空格,否则这样可以正常工作.
$('option[value='+popupSelect+']').remove();
Run Code Online (Sandbox Code Playgroud)
如果popupSelect = nospaces这将起作用.
如果popupSelect =有空格,这将无效,我将收到以下控制台错误:
未捕获错误:语法错误,无法识别的表达式:option [value = has spaces]
我尝试了各种报价安排,但似乎没有任何效果.
php ×3
arrays ×2
javascript ×2
amazon-s3 ×1
cloudflare ×1
foreach ×1
hotlinking ×1
jquery ×1
object ×1
xpath ×1