Jump to content

Preg_replace help


LemonInflux

Recommended Posts

I'm currently doing a project where I have a page, and on it are images and links.

 

The links look like this:

 

mysite.com/path/file.extension

 

I want them to look like this:

 

myothersite.com/mysite.com/path/file.extension

 

I thought, 'yeah, let's just use preg_replace and just alter any links to that'.

 

However, there's a problem. With any images on the page, I still want to keep it as

 

mysite.com/path/image.extension

 

This would mean I'd need to parse the <a> tags, and not the <img> tags. I thought of doing <a href="(.*?)"> to <a href="myothersite.com/\\1">, but then you can have classes and stuff in an <a> tag, so if the person did, say, <a class="foo" href="">, that wouldn't parse it. I don't want to parse out anything like href="mysite.com/path/file.extension", because if by chance that was written on the page, it wouldn't work. Does anyone have any idea how to get around this problem?

Link to comment
Share on other sites

I thought of doing <a href="(.*?)"> to <a href="myothersite.com/\\1">, but then you can have classes and stuff in an <a> tag, so if the person did, say, <a class="foo" href="">, that wouldn't parse it.

 

/<a[^>]+href="(.*?)"/. Of course, this assumes that double quotes are always used. Are you expecting single quotes and/or no quotes at all?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.