我在PHP中执行此功能以获取页面标题.我知道它可能看起来有点乱,但那是因为我是PHP的初学者.我preg_match("/<title>(.+)<\/title>/i",$returned_content,$m)之前在if中使用过它并没有像我预期的那样工作.
function get_page_title($url) {
$returned_content = get_url_contents($url);
$returned_content = str_replace("\n", "", $returned_content);
$returned_content = str_replace("\r", "", $returned_content);
$lower_rc = strtolower($returned_content);
$pos1 = strpos($lower_rc, "<title>") + strlen("<title>");
$pos2 = strpos($lower_rc, "</title>");
if ($pos2 > $pos1)
return substr($returned_content, $pos1, $pos2-$pos1);
else
return $url;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用上述功能获取以下页面的标题时,我得到的是:http: //www.google.com - >"302 Moved" http://www.facebook.com - >""http ://www.facebook.com" http://www.revistabula.com/posts/listas/100-links-para-clicar-antes-de-morrer - >"http://www.revistabula.com/posts/listas/100-links-para-clicar-antes-de-morrer"(当我添加一个/到链接的末尾,我可以成功获得标题:"100链接para clicar antes de morrer | Revista Bula")
我的问题是: - 当我尝试访问google.com时,我知道谷歌会重定向到我国家的镜像,但我怎样才能获得重定向到的页面标题? - 我的功能有什么问题让它获得某些页面的标题,而不是其他页面的标题?
从像这样的字符串<img src="/images/mylondon.jpg" />我试图检索JUST在PHP的其他地方使用的URL
我知道正则表达式是要走的路,但我现在无法理解它们.
有人可以帮忙吗?
可能重复:
如何使用PHP解析HTML?
我需要在td标记内解析一个字符串.我可以使用jQuery执行此操作,具体如下:
$("#right .olddata:first td.numeric:first").html()
Run Code Online (Sandbox Code Playgroud)
如果我在字符串变量中有HTML代码,我如何获得相同td的内容?
我正准备尝试使用Simple HTML DOM Framework进行抓取:http://simplehtmldom.sourceforge.net/但出于file_get_contents安全原因,在服务器配置中禁用了.
我现在需要找到一个使用Curl的类似框架 - 任何人都知道什么?
我尝试运行斜杠点示例时得到的错误消息是:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /var/www/vhosts/domain.com/httpdocs/crawlfeed/simple_html_dom.php on line 70
我正在尝试使用此处显示的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) 请帮助您解析PHP简单的html字符串(php regexp)。我需要从html代码中删除html-js事件。我知道php正则表达式非常糟糕。
代码示例:
<button onclick="..javascript instruction..">
结果: <button>
<button onclick="..javascript instruction.." value="..">
结果: <button value="..">
<button onclick=..javascript instruction..>
结果: <button>
<button onclick=..javascript instruction.. value>
结果: <button value>
我需要在不使用引号的情况下执行此操作,因为所有现代浏览器都允许执行属性而无需使用仲裁。
注意:我nedd不仅解析onclick ..都是从'on'开始的所有属性。
注意事项(2):不要尝试建议HTML解析器,因为它对于解析而言将是非常大的树。
更新:谢谢您的回复!现在,我使用由我编写的HTMLPurifier组件一个小框架。
我试图找出如何title="Title is here"在PHP中替换图像的标题部分(),但我不能让它工作,所以有人可以帮忙吗?
标题可以是字面上的任何东西,所以我需要找到title"{anything here}"并替换它(如下所示).
我正在向我们努力preg_replace(),但如果有更好的方法,我愿意接受建议.
我尝试了几种不同的变化,但我认为这并不是太远了 -
$pattern = '#^title="([a-zA-Z0-9])"$#';
$replacement = 'title="Visit the '.$service['title'].' page';
$service_image = preg_replace($pattern, $replacement, $service_image);
Run Code Online (Sandbox Code Playgroud) 我正在搜索一个函数来剪切以下字符串并获取所有内容BEFORE和AFTER
I need this part<!-- more -->and also this part
Run Code Online (Sandbox Code Playgroud)
结果应该是
$result[0] = "I need this part"
$result[1] = "and also this part"
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)
请帮助我尝试了很多时间,这也试过这个链接太匹配图像标签没有嵌套在锚标签使用正则表达式
php ×10
regex ×3
string ×2
dom ×1
element ×1
events ×1
html ×1
html-parsing ×1
image ×1
javascript ×1
jquery ×1
parsing ×1
title ×1
web-scraping ×1
xml ×1