php - only pulling one link out using regular expressions -
hi have following code , want pull out links have www. in them
<p> text < href="http://url.com/link/">link text</a> more text < href="http://www.anotehrurl.com/">more link text</a> , < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. more text < href="http://keepurl.co.uk/link/">link text</a> more text < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. < href="http://www.anotehrurl.com/">more link text</a></p>
im using reg expression:
(<a href="http:\/\/www.[\d\d]*?\/">([\d\d]*?)<\/a>)
basically want match link starts www , not match link keepurl.co.uk/.......
i have put through rubular , come out following:
{ result 1
1.< href="http://www.anotehrurl.com/">more link text 2.more link text result 2
1.< href="http://www.anotherurl.com/sub/sub/link.html">link text. more text < href="http://keepurl.co.uk/link/">link text 2.link text result 3
1.< href="http://www.anotherurl.com/sub/sub/link.html">link text. < href="http://www.anotherurl.com/sub/sub/link.html">link text. < href="http://www.anotehrurl.com/">more link text 2.more link text }
as u can see pulling out more want.
cheers
what this:
<?php $html = <<<end <p> text < href="http://url.com/link/">link text</a> more text < href="http://www.anotehrurl.com/">more link text</a> , < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. more text < href="http://keepurl.co.uk/link/">link text</a> more text < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. < href="http://www.anotherurl.com/sub/sub/link.html">link text</a>. < href="http://www.anotehrurl.com/">more link text</a></p> end; $r = '#href=\"http://(www\.[^\"]*)\">(.+)</#iu'; preg_match_all($r, $html, $m); var_dump($m[1]); var_dump($m[2]); ?>
output:
array 0 => string 'www.anotehrurl.com/' (length=19) 1 => string 'www.anotherurl.com/sub/sub/link.html' (length=36) 2 => string 'www.anotherurl.com/sub/sub/link.html' (length=36) 3 => string 'www.anotherurl.com/sub/sub/link.html' (length=36) 4 => string 'www.anotehrurl.com/' (length=19) array 0 => string 'more link text' (length=14) 1 => string 'link text' (length=9) 2 => string 'link text' (length=9) 3 => string 'link text' (length=9) 4 => string 'more link text' (length=14)
Comments
Post a Comment