Regular expressions - matching all anchors with optional attributes

I have a wysiwyg editor at my end and it disables the first regular expression that I wrote. This is in PHP4 using preg_replace() . I collect the URI and related text.

 @<a\shref=\"http[s]?://([^\"]*)\"[]>(.*)<\/a>@siU 

The client wanted all external links to open in a new window, so the expression that I used to find all (hopefully) external links, but leave internal links to the page anchor, etc.

I realized that the wysiwyg editor adds style="font-weight: bold" if the user selects a bold font by reference. I just recently started learning regular expressions, so I'm not sure how to solve this problem.

How can I do it?

+7
php regex
source share
1 answer

this should match this:

 /<a\s+([^>]*)href="https?:\/\/([^"]*)"(.*?)>(.*?)<\/a>/ 

A useful thing here is a lazy match. *? this means that he will only match as much as he needs, in contrast to the usual match, which is greedy.

To demonstrate using this text:

abcdabcd

these regular expressions will have different results:

 /a.*c/ selects: "abcdabc" /a.*?c/ selects: "abc" 
+7
source share

All Articles