LemonInflux Posted November 16, 2007 Share Posted November 16, 2007 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? Quote Link to comment Share on other sites More sharing options...
effigy Posted November 16, 2007 Share Posted November 16, 2007 If you use /<a[^>]+href=.../ it will find the "href" attribute no matter where it appears in the tag. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted November 16, 2007 Author Share Posted November 16, 2007 So how would that look as a whole example of a link parser? Quote Link to comment Share on other sites More sharing options...
effigy Posted November 16, 2007 Share Posted November 16, 2007 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? Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted November 16, 2007 Author Share Posted November 16, 2007 It could be any. Would I just use 3 different preg_replaces? Quote Link to comment Share on other sites More sharing options...
effigy Posted November 16, 2007 Share Posted November 16, 2007 /<a[^>]+href=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.