Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查找无结尾斜杠的url模式_Php_Preg Match All_Regex Negation - Fatal编程技术网

Php 查找无结尾斜杠的url模式

Php 查找无结尾斜杠的url模式,php,preg-match-all,regex-negation,Php,Preg Match All,Regex Negation,我正在寻找preg_match_all模式来查找页面上没有尾随斜杠的所有URL 例如:如果我有 a href=“/testing/abc/”>以斜杠结尾 a href=“/testing/test/mnl”>无结束斜杠 结果是#2 谢谢。最好使用DOM解析器提取所有href链接,看看URL是否以斜杠结尾。不需要正则表达式 对于所提供示例的正则表达式解决方案,您可以使用此正则表达式: /href=(['"])[^\s]+(?<!\/)\1/ /href=(['”])[^\s]+(? 现场演

我正在寻找preg_match_all模式来查找页面上没有尾随斜杠的所有URL

例如:如果我有

  • a href=“/testing/abc/”>以斜杠结尾

  • a href=“/testing/test/mnl”>无结束斜杠

  • 结果是#2


    谢谢。

    最好使用DOM解析器提取所有href链接,看看URL是否以斜杠结尾。不需要正则表达式

    对于所提供示例的正则表达式解决方案,您可以使用此正则表达式:

    /href=(['"])[^\s]+(?<!\/)\1/
    
    /href=(['”])[^\s]+(?
    现场演示:
    说明:
    
    href=->匹配文本href=
    (['”])->匹配单引号或双引号,并使用此匹配项创建组#1
    [^\s]+->匹配一个或多个字符,直到找到空格
    (?(负查找)仅当前面没有/
    \1->匹配结束单引号或双引号(组#1)
    
    最好使用DOM解析器提取所有href链接,看看URL是否以斜杠结尾。不需要正则表达式

    对于所提供示例的正则表达式解决方案,您可以使用此正则表达式:

    /href=(['"])[^\s]+(?<!\/)\1/
    
    /href=(['”])[^\s]+(?
    现场演示:
    说明:
    
    href=->匹配文本href=
    (['”])->匹配单引号或双引号,并使用此匹配项创建组#1
    [^\s]+->匹配一个或多个字符,直到找到空格
    (?(负查找)仅当前面没有/
    \1->匹配结束单引号或双引号(组#1)
    
    实际上,使用DOM解析器[]。下面是一个示例:

    // let's define some HTML
    $html = <<<'HTML'
    <html>
    <head>
    </head>
    <body>
        <a href="/testing/abc/">end with slash</a>
        <a href="/testing/test/mnl">no ending slash</a>
    </body>
    </html>
    HTML;
    
    // create a DOMDocument instance (a DOM parser)
    $dom = new DOMDocument();
    // load the HTML
    $dom->loadHTML( $html );
    
    // create a DOMXPath instance, to query the DOM
    $xpath = new DOMXPath( $dom );
    
    // find all nodes containing an href attribute, and return the attribute node
    $linkNodes = $xpath->query( '//*[@href]/@href' );
    
    // initialize a result array
    $result = array();
    
    // iterate all found attribute nodes
    foreach( $linkNodes as $linkNode )
    {
        // does its value not end with a forward slash?
        if( substr( $linkNode->value, -1 ) !== '/' )
        {
            // add the attribute value to the result array
            $result[] = $linkNode->value;
        }
    }
    
    // let's look at the result
    var_dump( $result );
    
    //让我们定义一些HTML
    $html=value,-1)!=='/'))
    {
    //将属性值添加到结果数组中
    $result[]=$linkNode->value;
    }
    }
    //让我们看看结果
    var_dump($结果);
    
    实际上,使用DOM解析器[]。下面是一个示例:

    // let's define some HTML
    $html = <<<'HTML'
    <html>
    <head>
    </head>
    <body>
        <a href="/testing/abc/">end with slash</a>
        <a href="/testing/test/mnl">no ending slash</a>
    </body>
    </html>
    HTML;
    
    // create a DOMDocument instance (a DOM parser)
    $dom = new DOMDocument();
    // load the HTML
    $dom->loadHTML( $html );
    
    // create a DOMXPath instance, to query the DOM
    $xpath = new DOMXPath( $dom );
    
    // find all nodes containing an href attribute, and return the attribute node
    $linkNodes = $xpath->query( '//*[@href]/@href' );
    
    // initialize a result array
    $result = array();
    
    // iterate all found attribute nodes
    foreach( $linkNodes as $linkNode )
    {
        // does its value not end with a forward slash?
        if( substr( $linkNode->value, -1 ) !== '/' )
        {
            // add the attribute value to the result array
            $result[] = $linkNode->value;
        }
    }
    
    // let's look at the result
    var_dump( $result );
    
    //让我们定义一些HTML
    $html=value,-1)!=='/'))
    {
    //将属性值添加到结果数组中
    $result[]=$linkNode->value;
    }
    }
    //让我们看看结果
    var_dump($结果);
    
    你怎么知道某个东西是一个URL?preg_match_all('#a href=“([^”]*[^/])“#i”)你怎么知道某个东西是一个URL?preg_match_all('#a href=“([^”]*[^/])它是有效的!你能简单解释一下吗。非常感谢。我正在尝试获取所有没有尾随斜杠的href链接,但不包括包含“images”text/path或.pdf的链接。我试过regex look Back,但没有成功。谢谢你的建议。既然这看起来像是一个新的要求,你可以提出一个问题,我很乐意提供答案。这是一个新问题:哦,谢谢,看起来有人回答了你的问题。请让我知道,如果这不能解决你的问题,那么我也可以为你找到一个正则表达式。它的工作!你能简单解释一下吗。非常感谢。我正在尝试获取所有没有尾随斜杠的href链接,但不包括包含“images”text/path或.pdf的链接。我试过regex look Back,但没有成功。谢谢你的建议。既然这看起来像是一个新的要求,你可以提出一个问题,我很乐意提供答案。这是一个新问题:哦,谢谢,看起来有人回答了你的问题。请让我知道,如果这不能解决你的问题,那么我也可以为你找到一个正则表达式。