我有一个HTML文件,其中包含以下内容:
<img src="MATCH1" bla="blabla">
<something:else bla="blabla" bla="bla"><something:else2 something="something">
<something image="MATCH2" bla="abc">
Run Code Online (Sandbox Code Playgroud)
现在我需要一个正则表达式匹配MATCH1和MATCH2
此外,HTML包含多个这样的部分,因此它可以在HTML的1,2,3中x次.
当我说:
<img\s*src="(.*?)".*?<something\s*image="(.*?)"
Run Code Online (Sandbox Code Playgroud)
它与它不匹配.我在这里错过了什么?
提前致谢!
我试图从我的PHP脚本下载的文件就是这个:
http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml
Run Code Online (Sandbox Code Playgroud)
但我不能既不使用file_get_contents()也不使用cURL.我收到了错误Object reference not set to an instance of an object.
知道怎么做吗?
非常感谢,巴勃罗.
更新以添加代码:
$url = "http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml";
$simple = simplexml_load_file(file_get_contents($url));
foreach ($simple->farmacia as $farmacia)
{
var_dump($farmacia);
}
Run Code Online (Sandbox Code Playgroud)
而且该解决方案由于@Gordon:
$url = "http://www.navarra.es/appsext/DescargarFichero/default.aspx?codigoAcceso=OpenData&fichero=Farmacias/Farmacias.xml";
$file = file_get_contents($url, FALSE, stream_context_create(array('http' => array('user_agent' => 'php' ))));
$simple = simplexml_load_string($file);
Run Code Online (Sandbox Code Playgroud) 如何匹配以下HTML代码中的所有<li>标记:
<ul>
<li> some content</li>
<li> some other content</li>
<li> some other other content.</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
此表达式不起作用:
<LI>(.*)</ LI>
因为它返回:
some content</li>
<li> some other content</li>
<li> some other other content.
Run Code Online (Sandbox Code Playgroud)
这是第一个<li>和最后一个</ li>之间的内容
我正试图从我的HTML中获取Facebook的元标记.
我正在使用简单的html dom从网站获取所有html数据.我试过preg_replace,但没有运气.
我希望例如获取此fb元标记的内容:
<meta content="IMAGE URL" property="og:image" />
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮忙!:-)
如何用每个锚文本替换所有锚。我的代码是
$body='<p>The man was <a href="http://www.example.com/video/">dancing like a little boy</a> while all kids were watching ... </p>';
Run Code Online (Sandbox Code Playgroud)
我希望结果是:
<p>The man was dancing like a little boy while all kids were watching ... </p>
Run Code Online (Sandbox Code Playgroud)
我用了 :
$body= preg_replace('#<a href="https?://(?:.+\.)?ok.co.*?>.*?</a>#i', '$1', $body);
Run Code Online (Sandbox Code Playgroud)
结果是:
<p>The man was while all kids were watching ... </p>
Run Code Online (Sandbox Code Playgroud) 我正在使用这个简单的PHP HTML解析器:http://simplehtmldom.sourceforge.net.是否可以使用它来回显HTML规范的所有标记?
有没有办法(使用XPath和PHP)执行以下操作(没有外部XSLT文件)?
我在这里收到了一个XSLT答案,但我正在寻找不需要外部文件的XPATH查询.
目前,我通过以下方式将有问题的HTML加载到SimpleXmlElement中:
$doc = @DOMDocument::loadHTML($xml);
$data = simplexml_import_dom($doc);
Run Code Online (Sandbox Code Playgroud)
现在我需要帮助:
$data = $data->xpath('??????');
Run Code Online (Sandbox Code Playgroud)
一直在使用这个几天无济于事.我非常感谢你的帮助.
编辑:我并不特别在意段落内的内容,因为我可以使用strip_tags来消除我不想要的内容.我需要做的就是将段落与其他来源隔离开来.我想更具体,更准确的要求是:
仅返回未包含在表中且仅在第一个h1标记之前的段落(及其html内容)
编辑2:
我想我已经完成了大部分工作:
$query = $xpath->query('//p[not(ancestor::table) and not(preceding::h2)]');
唯一的问题是内部HTML的丢失.
我正在尝试使用此处显示的PHP代码解析下面的XML,但是当它运行时我最终得到一个空白屏幕.我一直在寻找类似问题和其他类似响应的stackoverflow,但我仍然无法让它工作.
<?php
$url = 'xml-file.xml';
$xml = simplexml_load_file($url);
// get first book title
$title=$xml->entry[0]->name;
// show title
echo $title;
echo '<br/>';
// get second book title
$title=$xml->entry[1]->name;
// show title
echo $title;
echo '<br/>';
?>
Run Code Online (Sandbox Code Playgroud)
这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="shopping#products" gd:etag=""lm_25heFT8yiumci9EH1kItJBpg/bXZf0ylJ3aJceGcE1I8C1fJbzgo"" xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:s="http://www.google.com/shopping/api/schemas/2010">
<id>tag:google.com,2010:shopping/products</id>
<updated>2012-01-27T13:41:20.177Z</updated>
<title>Shopping Products</title>
<generator version="v1" uri="https://www.googleapis.com/shopping/search/">Search API for Shopping</generator>
<link rel="alternate" type="text/html" href="https://www.googleapis.com/shopping/search/"/>
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products?alt=atom"/>
<link rel="self" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=digital+camera&alt=atom&startIndex=1&maxResults=25"/>
<link rel="next" type="application/atom+xml" href="https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=digital+camera&alt=atom&startIndex=26&maxResults=25"/>
<link rel="previous" type="application/atom+xml"/>
<openSearch:totalResults>6446790</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry gd:kind="shopping#product">> …Run Code Online (Sandbox Code Playgroud) 我是正则表达式的新手,我尝试了很多用于获取锚标记内的图像标记值html这是我的html表达
<div class="smallSku" id="ctl00_ContentPlaceHolder1_smallImages">
<a title="" name="http://www.playg.in/productImages/med/PNC000051_PNC000051.jpg" href="http://www.playg.in/productImages/lrg/PNC000051_PNC000051.jpg" onclick="return showPic(this)" onmouseover="return showPic(this)">
<img border="0" alt="" src="http://www.playg.in/productImages/thmb/PNC000051_PNC000051.jpg"></a> <a title="PNC000051_PNC000051_1.jpg" name="http://www.playg.in/productImages/med/PNC000051_PNC000051_1.jpg" href="http://www.playg.in/productImages/lrg/PNC000051_PNC000051_1.jpg" onclick="return showPic(this)" onmouseover="return showPic(this)">
<img border="0" alt="PNC000051_PNC000051_1.jpg" src="http://www.playg.in/productImages/thmb/PNC000051_PNC000051_1.jpg"></a>
</div>
Run Code Online (Sandbox Code Playgroud)
我想只返回图像标签的src值,我尝试了"preg_match_all()"中的匹配模式,模式是
"@<div[\s\S]class="smallSku"[\s\S]id="ctl00_ContentPlaceHolder1_smallImages"\><a title=\"\" name="[\w\W]" href="[\w\W]" onclick=\"[\w\W]" onmouseover="[\w\W]"\><img[\s\S]src="(.*)"[\s\S]></a><\/div>@"
Run Code Online (Sandbox Code Playgroud)
请帮助我尝试了很多时间,这也试过这个链接太匹配图像标签没有嵌套在锚标签使用正则表达式