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. For example I want to check
<a href="http://www.e开发者_Go百科xample.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.
I want to check the codes with regex. The Regex should check href=""
, title=""
and between the a tags (<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:
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.
精彩评论