相关疑难解决方法(0)

解析URL的网站

只是想知道是否有人可以通过以下方式进一步帮助我.我想解析这个网站上的URL:http://www.directorycritic.com/free-directory-list.html?pg = 1&sort = pr

我有以下代码:

<?PHP  
$url = "http://www.directorycritic.com/free-directory-list.html?pg=1&sort=pr";
$input = @file_get_contents($url) or die("Could not access file: $url"); 
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
if(preg_match_all("/$regexp/siU", $input, $matches)) { 
// $matches[2] = array of link addresses 
// $matches[3] = array of link text - including HTML code
} 
?>
Run Code Online (Sandbox Code Playgroud)

目前没有做什么,我需要做的是废弃所有16页的表中的所有URL,并且非常感谢一些帮助,如何修改上述内容并将URL输出到文本文件中.

html php parsing html-parsing

5
推荐指数
1
解决办法
6452
查看次数

Regexp用于从HTML中提取所有链接和锚文本

我想要一个或多个正则表达式可以:

1)获取大页面的html.

2)查找所有链接中包含的URL,例如:

<a href="http://example1.com">Test 1</a>
<a class="foo" id="bar" href="http://example2.com">Test 2</a>
<a onclick="foo();" id="bar" href="http://example3.com">Test 3</a>
Run Code Online (Sandbox Code Playgroud)

依此类推,它应该提取'href'属性中包含的url,而不管之前或之后是什么href

3)提取所有链接的锚文本,例如在上面的例子中,它应该返回'http://example1.com'和锚文本'Test 1',然后'http://example2.com'和'测试2',依此类推.

php regex string html-parsing

3
推荐指数
3
解决办法
2万
查看次数

使用domDocument和解析信息,我想获得'a'标签的'href'内容

可能重复:
用于获取A元素的href属性的正则表达式

这显示了a标签之间的内容,但我想要一种获取href内容的方法.

有没有办法使用domDocument做到这一点?

$html = file_get_contents($uri);
$html = utf8_decode($html);

/*** a new dom object ***/
$dom = new domDocument;

/*** load the html into the object ***/
@$dom->loadHTML($html);

/*** discard white space ***/
$dom->preserveWhiteSpace = false;

/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('table');

/*** get all rows from the table ***/
$rows = $tables->item(0)->getElementsByTagName('tr');

/*** loop over the table rows ***/
foreach ($rows as $row)
{
    $a = $row->getElementsByTagName('a');
    /*** echo …
Run Code Online (Sandbox Code Playgroud)

php domdocument

2
推荐指数
1
解决办法
4788
查看次数

在"href"标记中解析包含特定单词的所有链接

可能重复:
抓取A元素的href属性

我需要解析包含某些单词的HTML文档的所有链接(它总是不同的).

例:

<a href="/bla:bla">BLA</a>
<a href="/link:link">BLA</a>
<a href="/link:bla">BLA</a>
Run Code Online (Sandbox Code Playgroud)

我只需要带有"href =/link:...."的链接,最好的方法是什么?

$html = "SOME HTLM ";
$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
foreach ($urls as $url)
{
    echo "<br> {$url->getAttribute('href')} , {$url->getAttribute('title')}";
    echo "<hr><br>";
}
Run Code Online (Sandbox Code Playgroud)

在此示例中显示了所有链接,我需要特定的链接.

php parsing

2
推荐指数
1
解决办法
3292
查看次数

将_blank添加到所有外部链接

可能重复:
抓取A元素的href属性
解析包含"href"标记中特定单词的所有链接

我正在使用以下函数将_blank添加到我网站上的所有链接.

function targetBlank($text) {
  $return = str_replace('<a', '<a target="_blank"', $text);
  return $return;
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种解决方案,只在外部链接(不在我的域上)而不是所有链接上应用此功能.

php uri str-replace

2
推荐指数
1
解决办法
3167
查看次数

获取img标签,它的src值

我有 2 个图像标签,一个接一个

<img class="c1 c2 c3" title="Image Title 1" src="http://example.com/image-1.jpg" alt="" width="620" height="521"><img class="c1 c2 c3" title="Image Title 2" src="http://example.com/image-2.jpg" alt="" width="620" height="521">
Run Code Online (Sandbox Code Playgroud)

我想要一个可以获取两件事的正则表达式:

  • 第一个“img”标签
  • 第一个“img”标签中的“src”值

我该怎么做?

PS有人知道我可以在哪里在线测试正则表达式吗

php regex

0
推荐指数
1
解决办法
5493
查看次数

完美的preg_match_all来获取href

可能重复:
抓取A元素的href属性

我已经浏览了很多其他帖子,看到所有帖子都遵循一些固定的Anchor标签格式.他们中的大多数人都认为锚标签格式是<a href="http://www.example.com/">Hello</a>..也许是target后面的属性<a..但我正在尝试编写一个正则表达式来匹配锚标记的href,无论它出现在标记中的哪个位置.它可以出现在alt,title,target之后或者它们之间.还有另一种情况,锚标记使用单引号而不是双引号.
我已经尝试了半个小时而没有得到任何结果.所以在这里发布.

php preg-match-all

0
推荐指数
1
解决办法
191
查看次数

使用preg_match在字符串中查找img标记

如何在PHP中找到字符串中的HTML标记?

这是我的数据库中的记录:

$str = "This is me <img src='images/mardagz.png' width='200' /> :) when i was 4th year highschool hahah so funny...";
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取<img>标签的内容.这是我正在使用的代码:

$gimg = preg_match('/<img[^>]+\>/i', $str , $matches) ? $matches[1]: '<img src="http://facebook.com/username/profile.png" width="200" />
Run Code Online (Sandbox Code Playgroud)

但是,这段代码总是给我这个错误: Notice: Undefined offset: 1 in mardagz.blog\post.php on line 19

我该怎么办?

php regex

0
推荐指数
1
解决办法
2万
查看次数

从HTML中提取文本

可能重复:
抓取A元素的href属性

我正在尝试从HTML页面中提取一些文件夹名称,HTML的源代码看起来像这样.

<li><a href="/"> Parent Directory</a></li>
<li><a href=".ftpquota"> .ftpquota</a></li>
<li><a href="Folder%201/"> Folder 1/</a></li>
<li><a href="Floder%202/"> Folder 2/</a></li>
<li><a href="Folder%20N/"> Folder N/</a></li>
Run Code Online (Sandbox Code Playgroud)

到目前为止我创建的内容,我可以提取一些文件夹,但没有正确提取.

这就是我所做的......

<?php

    $url = "URL";
    $page_data = file_get_contents($url);
    $search_pattern = "<li><a href=";
    $position = 0;

    while($position = strpos($page_data,$search_pattern, $position+strlen($search_pattern)))
    {
        //$pos2 = strpos($page_data, "\"> ", $position);
        //echo $position . " - " . $pos2 . " = " . ($pos2-$position) . "<br />";
        $str = substr($page_data,$position+strlen($search_pattern)+1, $pos2-$position);
        echo "<pre>" . $position . " || " . $str …
Run Code Online (Sandbox Code Playgroud)

php parsing

-2
推荐指数
1
解决办法
123
查看次数

PHP正则表达式匹配除某些src之外的所有img标记

我是PHP的新手,对用PHP编写的CMS中的某个文件进行了一些修改.我修改了一个<img>在页面源中获取第一个标记的函数,从该源获取随机标记.

用于匹配源的正则表达式是:

$regex = '/<' . $tag . '\\b[^>]*>/i';
Run Code Online (Sandbox Code Playgroud)

其中$tag只包含一个字符串img.

但是我注意到在源代码中有图像,其中src属性包含"1px.gif",我不想匹配这些.

目前我不断从匹配数组中重新选择一个随机元素,直到它不是1px.gif,但当然这是一个糟糕的解决方案.

我不能用正则表达式自己做这个,但我理解上面的正则表达式搜索<img和一个不是的单词字符>.我需要添加"并且不包含'1px.gif'".

我可以选择检查匹配数组并删除每个1px.gif的条目,但我更喜欢正则表达式.

php regex xpath html-parsing domdocument

-2
推荐指数
1
解决办法
691
查看次数