【问题标题】:preg_match for a specific pattern in a url?url中特定模式的preg_match?
【发布时间】:2014-04-01 04:11:06
【问题描述】:

我想在 PHP 中使用preg_match 来测试 URL 的格式。网址如下所示:

<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>

老实说,我不知道preg_match 创建,但我的目标是

&lt;a href= contain word ~dead host~ end with &lt;/a&gt;开头的模式

我尝试将字符串包含在 php 本机函数中,但不幸的是它并不聪明,所以我认为 preg_match 是唯一的选择。

【问题讨论】:

  • 如果是处理HTML数据,最好使用DOM
  • 感谢您的建议,但 preg_match 将是最佳选择
  • 是的,你是对的,在我的情况下,当你 prase 大量数据时它很有用我有 10 个大约链接,其中一些包含 ~dead host~ 字符串经过仔细考虑后我决定 preg_match 将是最好的,所以为什么我正在努力

标签: php html-parsing preg-match


【解决方案1】:

如果你只想匹配 url

$text="<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>";

 preg_match_all("/http:\/\/ ## starting from http://
 ~dead\shost~   ## along with http:// match ~dead host~
 [^\"']         ## upto singlequote or doublequote  
 +              ## one more character  
 /mx",$text,$matches);   //  m - multiple line x - include to commentary inside patterns
 print_r($matches);

工作Demo

【讨论】:

    【解决方案2】:

    我并不完全清楚您的文本是什么样的,以及您想要匹配的内容,但我会尽力做到正确。

    基本上我在这里做的是寻找一个打开的链接标签&lt;a,然后是一些东西(除了结束HTML标签之外的任何东西),然后是用tildas ~包裹的文本dead host。然后是更多内容,然后是结束链接标签&lt;/a&gt;

    $string = "<a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>";
    
    if (preg_match('%<a[^>]*?~dead host~.*?</a>%i', $string)) {
        print "Circle up the wagons - a match was found!";  
    }
    else {
        print "Let's pitch camp here - no match was found!";
    }
    

    这里是对正则表达式的解释:

    %   <a   [^>]*?   ~dead host~   .*?   </a>   %   i
    ^    ^      ^          ^         ^      ^    ^   ^
    1    2      3          4         5      6    7   8
    
    1. % Delimiter - 告诉脚本模式从这里开始。
    2. &lt;a 寻找打开链接标签。
    3. [^&gt;]*? 这是一个字符类 [] 告诉脚本找到任何不是 ^ 结束 html 标记 &gt; 的字符,尽可能多次 *,直到你点击下一部分表达式?。在这种情况下,它会在找到~dead host~ 时停止。这类似于第 5 项,除了我们希望它匹配除 HTML 结束标记之外的任何字符,而在第 5 项中,它可以匹配任何字符,包括结束 HTML 标记。
    4. ~dead host~ 查找包含在 tildas '~' 中的文字字符串 'dead host'。
    5. .*? 这意味着找到任何字符.,尽可能多地找到*,直到你点击表达式? 的下一部分。在这种情况下,它是&lt;/a&gt;
    6. &lt;/a&gt; 寻找结束链接标签。
    7. % Delimiter - 告诉脚本模式到此结束。
    8. i 模式修饰符 - 告诉脚本忽略大小写。如果您正在搜索多行而不是仅一行,您可能还需要添加 ms 标志。因此,您的模式修饰符不是像这样:i,而是像这样:ims。虽然这在技术上并不正确,但一般而言,这具有将文本视为一行的效果,即使您有多行也是如此。

    希望这就是您想要的。如果我对您要查找的内容的理解不正确,请告诉我,我可以进行编辑以调整它以获得您想要的内容。

    Here is a working demo

    编辑:

    针对您的评论,您可以使用preg_replace 代替preg_match 来替换内容。

    $string = " 
    
    <a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
    <a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
    <a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://stackoverflow.com' rel='nofollow' target='blank'>part-2</a><a href='http://~dead host~/vypdye57f25o' rel='nofollow' target='blank'>part-2</a>
    
    ";
    
    $string = preg_replace('%<a[^>]*?~dead host~.*?</a>%i', ' ', $string);
    
    print $string;
    

    这将用空格替换所有匹配项,而不是仅仅匹配它们。

    Here is a working demo of the replacement

    【讨论】:

    • 我看到了你的回复。首先我很惊讶。这太简短和完整了。我不确定,但声称如果我付钱给别人,没有人能像你写的那样写。这个代码是完整的,我正在按照我的想法工作。不需要对其进行任何更改 p.s:任何想法是否有 100 个链接,其中一半就像字符串(高于预匹配),我想用空格替换,例如 pastebin.com/CfQdrihS
    • 是的,这很容易做到。不要使用preg_match,而是使用preg_replace。我已对上面的代码进行了编辑,以向您展示如何操作。
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多