I am looking for a regular expression in PHP which would match the anchor with a specific text on it. E.g I would like to get anchors with text mylink like:
This should work (build the regex string and insert whatever string you need instead of "mylink")
<\s*a\s+[^>]*>[^<>]*mylink[^<>]*<\s*\/a\s*>
But this is not recommended. You should use an HTML parser instead and process the tag. Regex is not really the right tool for this. (The above regex will not work if you have links that contain ">" although that might be rare)
I presume php doesnt require any special escape characters if you just use the appropriate wrap around.
Tested at regexpal.com
A few notes::
\s* - To match optional whitespace
\s+ - To match atleast one space/tab and any extra optional whitespace
[^>] - Matches any character except '>'
[^<>]- Matches any character except '<' or '>'
UPDATE: escaped the "/" for php matching with m/regex/