如何解析HTML/XML并从中提取信息?
我对正则表达式不是很好,但是使用PHP我想要style从TinyMCE中返回的字符串中删除HTML标签中的属性.
所以转变<p style="...">Text</p>为香草<p>Test</p>.
我将如何通过preg_replace()功能实现这一目标?
有没有更好的方法来解析无效的HTML然后应用Tidy?
旁注:有些情况下你不能提供Tidy.我还不建议使用Regexp来解析html.
我需要以几种不同的方式处理html字符串中的链接.
$str = 'My long <a href="http://example.com/abc" rel="link">string</a> has any
        <a href="/local/path" title="with attributes">number</a> of
        <a href="#anchor" data-attr="lots">links</a>.'
$links = extractLinks($str);
foreach ($links as $link) {
    $pattern = "#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie";
    if (preg_match($pattern,$str)) {
        // Process Remote links
        //   For example, replace url with short url,
        //   or replace long anchor text with truncated
    } else {
        // Process Local Links, Anchors
    }
}
function extractLinks($str) {
    // First, I tried DomDocument
    $dom = new DomDocument();
    $dom->loadHTML($str);
    return $dom->getElementsByTagName('a');
    // But this …好吧..所以基本上,说我们有一个链接:
$url = "http://www.site.com/index.php?sub=Mawson&state=QLD&cat=4&page=2&sort=z";
基本上,我需要创建一个函数,它替换URL中的每个东西,例如:
<a href="<?=$url;?>?sort=a">Sort by A-Z</a>
<a href="<?=$url;?>?sort=z">Sort by Z-A</a>
或者,另一个例子:
<a href="<?=$url;?>?cat=1">Category 1</a>
<a href="<?=$url;?>?cat=2">Category 2</a>
或者,另一个例子:
<a href="<?=$url;?>?page=1">1</a>
<a href="<?=$url;?>?page=2">2</a>
<a href="<?=$url;?>?page=3">3</a>
<a href="<?=$url;?>?page=4">4</a>
基本上,我们需要一个函数来替换$_GETURL中的特定内容,这样我们就不会得到重复内容,例如:?page=2&page=3
话虽如此,它需要是智能的,所以它知道参数的开头是a ?还是a&
我们还需要它是聪明的,以便我们可以像这样拥有URL:
<a href="<?=$url;?>page=3">3</a> (without the ? - so it will detect automatically wether to use an `&` or a `?`
我不介意为每个preg_replace为某些$ _GET参数创建不同的变量,但我正在寻找最佳方法.
谢谢.
当我执行以下代码时; 我每次都会遇到一个段错误!这是一个已知的错误?如何使此代码有效?
<?php
$doc = file_get_contents("http://prairieprogressive.com/");
$replace = array(
    "/<script([\s\S])*?<\/ ?script>/",
    "/<style([\s\S])*?<\/ ?style>/",
    "/<!--([\s\S])*?-->/",
    "/\r\n/"
);
$doc = preg_replace($replace,"",$doc);
echo $doc;
?>
错误(显然)看起来像:
[root@localhost 2.0]# php test.php
Segmentation fault (core dumped)
使用PHP和preg_match_all我试图获取以下标记之间的所有HTML内容(以及标记):
<p>paragraph text</p>
don't take this
<ul><li>item 1</li><li>item 2</li></ul>
don't take this
<table><tr><td>table content</td></tr></table>
我可以得到其中一个就好了:
preg_match_all("(<p>(.*)</p>)siU", $content, $matches, PREG_SET_ORDER);
有没有办法让所有的
<p></p> <ul></ul> <table></table>
内容只有一个preg_match_all?我需要它们按照它们被发现的顺序出来,所以我可以回应内容,这将是有意义的.
所以,如果我在上面的内容上做了一个preg_match_all,那么迭代通过$ matches数组就会回显:
<p>paragraph text</p>
<ul><li>item 1</li><li>item 2</li></ul>
<table><tr><td>table content</td></tr></table>
如何获取具有特定类名的所有行,例如:
<tr class="dailyeventtext" bgcolor="#cfcfcf" valign="top">
然后将该行中的每个单元格放入一个数组中?
我使用cURL从客户端的服务器上获取页面.
我有一个HTML文件,其中包含以下内容:
<img src="MATCH1" bla="blabla">
<something:else bla="blabla" bla="bla"><something:else2 something="something">
<something image="MATCH2" bla="abc">
现在我需要一个正则表达式匹配MATCH1和MATCH2
此外,HTML包含多个这样的部分,因此它可以在HTML的1,2,3中x次.
当我说:
<img\s*src="(.*?)".*?<something\s*image="(.*?)"
它与它不匹配.我在这里错过了什么?
提前致谢!
如何通过PHP获取页面内容?我如何获取博客文章的文本,因为大多数RRS提要仅提供文章的链接,所以我不能使用它.是否存在PHP函数或者无论如何都要执行此操作.请提供一些建议:).
php ×10
regex ×4
html ×2
html-parsing ×2
parsing ×2
preg-replace ×2
domdocument ×1
html-table ×1
preg-match ×1
query-string ×1
tinymce ×1
xml ×1
xml-parsing ×1