Convert Relative Paths to Absolute Using Regular Expressions (code.nontalk.com)

submitted by rimsystemsrimsystems(6119) 4 years, 8 months ago

I ran into a situation where I needed to screen scrape some content from a site and display it on my own site. This works really well except for dependent files like javascripts, SWFs and images that had src attributes with relative paths. So I figured it wouldn't be that hard to create a helper method to find and replace them using Regular Expressions.

2 comments |category: |Views: 134

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by aquinasaquinas(20) 4 years, 8 months ago 0

The code is wrong.

It will not output

img src="http://www.nontalk.com/images/dot.gif, it will output img src="http://www.nontalk.com/mages/dot.gif because it says: match src|href followed by any character that is not h, t, or p. If the url started with h or t or p, the regex wouldn't match at all. I think he wanted a negative lookahead rather than a negated character class. E.g., (?!http)

Reply

posted by nontalknontalk(0) 4 years, 8 months ago 0

good catch! You are right. I updated the code.

Reply

information Login or create an account to comment on this story