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
https://forums.phpfreaks.com/topic/77652-preg_replace-help/
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
https://forums.phpfreaks.com/topic/77652-preg_replace-help/#findComment-393094
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.