Stripping whitespace and dot from hyperlinks

I am trying to remove whitespace and dot from hyperlinks all rules are working fine except its not removing dot from url. Here are few examples

 <a href="   http://www.example.com   ">example site</a>
 <a href="   http://www.example.com">example 2</a>
 <a href="http://www.example.com.">final example</a>


  $text = preg_replace('/<a href="([\s]+)?([^ "\']*)([\s]+)?(\.)?">([^<]*)<\/a>/', '<a href="\\2">\\5</a>', $text);

In the last example RE should remove dot from url. Dot is optional so I wrote this rule (.)?

Answers


Because your dot is already matched by ([^ "\']*) group.

Change it to ([^ "\']*?) - ungreedy version.

And also I suggest you to replace ([\s]+)?(\.)? with [\s.]* to handle "www.example.com.    " strings.


How about <a href="([\s]+)?([^ "\']*\.[a-zA-Z]{2,5})([\s]+)?(\.)?">([^<]*)<\/a>? .[a-zA-Z]{2,5}?

It will catch .com, .info, .edu and even something like .com.au


This will trim up the hrefs (I asume you mean to trim them).

for both '" value delimeters (expanded):

(<a \s+ href \s* = \s*)
(?|
     (") \s* ([^"]*?) [\.\s]* (")
  |  (') \s* ([^']*?) [\.\s]* (')
)
([^>]*>)

replacement is: $1$2$3$4$5

or,

for just " value delimeter (expanded):

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

replacement is: $1$2$3


Need Your Help

JavaFX HTML Editor: Text is in one single line

java io javafx-8

i am using the following method to read from a file:

Mogenerator error: skipping entity MyObjectName (NSManagedObject) because it doesn't use a custom subclass.

xcode core-data mogenerator

Why am I seeing the following error when running mogenerator to generator class files for my Core Data model objects?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.