Home »
Imported messages »
comp.lang.php »
Please test/review my Regex to locate hyperlink in text
Please test/review my Regex to locate hyperlink in text [message #172485] |
Mon, 21 February 2011 06:06 |
Simon
Messages: 29 Registered: February 2011
Karma:
|
Junior Member |
|
|
Hi,
My requirements are as follow,
1) Find all hyperlinks in a given text document...
2) Parse for certain links and replace them if need be, (in another
parser function).
3) Any attributes given in the hyperlink can be ignored and will be
faithfully returned in the matches.
4) All JavaScript and the likes are pre-stripped so the text can be
assumed to be 'safe', (if it is not safe then it is not the job of this
regex to handle it).
// -----------------------------------
// my pattern...
$pattern = '/<a (.*?)href=[\"\']??(.*?)\/\/(.*?)[\s\"\'](.*?)>(.*?)<\/a>/i';
// the call back function
$body = preg_replace_callback($pattern, 'my_parser', $body);
// -----------------------------------
The way I see it this should work for...
- <a href='example.com'>some text</a>
- <a href="example.com">some text</a>
- <a href=example.com>some text</a>
- <a href='http://example.com'>some text</a>
- <a href="http://example.com">some text</a>
- <a href=http://example.com>some text</a>
- <a href='example.com' tagret=_blank>some text</a>
- <a href="example.com" tagret=_blank>some text</a>
- <a href=example.com tagret=_blank>some text</a>
- <a href='http://example.com' tagret=_blank>some text</a>
- <a href="http://example.com" tagret=_blank>some text</a>
- <a href=http://example.com tagret=_blank>some text</a>
Can you poke holes in my regex please :)
Any suggestions/better regexs?
Many thanks
Simon
|
|
|
Goto Forum:
Current Time: Tue Nov 26 02:26:13 GMT 2024
Total time taken to generate the page: 0.04232 seconds