简体   繁体   English

检查给定的代码是否正确? 通过正则表达式检查(PHP)

[英]Check The Given Code Is Correct? Check by Regex(PHP)

I'm providing a backlink selling service, but I have problem while checking the sold links on publisher web sites.我正在提供反向链接销售服务,但在检查发布者 web 网站上的销售链接时遇到问题。 For example I want to check例如我想检查

<a href="http://www.example.com" title="example">example</a>

I can check this but some users add target="_blank" some of them target="_new" ... The code's structure is changing by the webmaster.我可以检查一下,但有些用户添加了target="_blank"其中一些是target="_new" ...代码的结构正在由网站管理员更改。

I want to check the codes with regex.我想用正则表达式检查代码。 The Regex should check href="" , title="" and between the a tags ( <a>here</a> ).正则表达式应该检查href=""title=""和 a 标签之间( <a>here</a> )。

I've made you this:我给你做了这个:

$str = "<a onclick=\"foo()\" href=\"http://www.example.com\" title=\"example\">example</a>" ;

function url_grab( $html )
{
    preg_match( "/<a\s+.*href=(\"|')([^\\1]+)(\\1).*>(.+)<\/a>/U" , $html , $m ) ;
    return array( $m[ 2 ] , $m[ 4 ] ) ;
}

// test it
var_dump( url_grab( $str ) ) ;

output: output:

array(2) {
  [0]=>
  string(22) "http://www.example.com"
  [1]=>
  string(7) "example"
}

Use this parser instead of regular expressions, which are cool but the wrong tool for this job.使用这个解析器而不是正则表达式,这很酷,但对于这项工作来说是错误的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM