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? Link to comment https://forums.phpfreaks.com/topic/77652-preg_replace-help/ 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. Link to comment https://forums.phpfreaks.com/topic/77652-preg_replace-help/#findComment-393088 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? Link to comment https://forums.phpfreaks.com/topic/77652-preg_replace-help/#findComment-393093 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? Link to comment https://forums.phpfreaks.com/topic/77652-preg_replace-help/#findComment-393094 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? Link to comment https://forums.phpfreaks.com/topic/77652-preg_replace-help/#findComment-393095 Share on other sites More sharing options...
effigy Posted November 16, 2007 Share Posted November 16, 2007 /<a[^>]+href=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/ Link to comment https://forums.phpfreaks.com/topic/77652-preg_replace-help/#findComment-393099 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.