xionhack Posted July 12, 2010 Share Posted July 12, 2010 Hello. I have a project that is a little bit complicated for me. I want to give the source of an html site as a string, check that string and save all the links in "href" to a database, then replace those links with the id that the database would give. For example: <html> <body> Hello, my name is <a href="http://www.link1.com">Xion</a>, and I'm trying to program in <a id="link" href="http://www.php.net">PHP</a> </body> </html> What it would do is to save those two links in a database, into something like +------------------+------------------------------+ | Link_id | LINK | +------------------+------------------------------+ | 1 | http://www.link1.com | +------------------+------------------------------+ | 2 | http://www.php.net | +------------------+------------------------------+ And then swap the site with: <html> <body> Hello, my name is <a href="1">Xion</a>, and I'm trying to program in <a id="link" href="2">PHP</a> </body> </html> IM STUCK!!! Can anybody help me? Thanks! Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/ Share on other sites More sharing options...
gwolgamott Posted July 12, 2010 Share Posted July 12, 2010 what have you tried so far? Write it to database, get from database and echo parts of the website with the links. Straight forward approach, but a lot of complicated work when you get down to it. Start with that and then ask when you are stuck on something particular. You'd have to pay someone to write code for you. Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/#findComment-1084828 Share on other sites More sharing options...
Wolphie Posted July 12, 2010 Share Posted July 12, 2010 The PHP documentation is amazing, by the way. http://php.net/manual/en/function.file-get-contents.php http://php.net/manual/en/function.preg-replace.php Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/#findComment-1084839 Share on other sites More sharing options...
Wolphie Posted July 12, 2010 Share Posted July 12, 2010 This should work (the file_exists() function will not work on a URL) <?php function create_link($matches) { // link_id should be an auto-increment field $insert = mysql_query(sprintf("INSERT INTO links ( link_url ) VALUES ( '%s' )", $matches[1])); return '<a href="' . mysql_insert_id() .'">'. $matches[2] .'</a>'; } // File name, could be a url $file = 'file.html'; if (file_exists($file)) { $content = file_get_contents($file); // Regex replace string $pattern = '/<a href="([^"]+)">([^<]+)<\/a>/s'; $content = preg_replace_callback($pattern, 'create_link', $content); echo $content; } ?> Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/#findComment-1084851 Share on other sites More sharing options...
xionhack Posted July 20, 2010 Author Share Posted July 20, 2010 Hi, thank you so much, it works like a charm! the only problem that it gives me is that if for example, instead of <a href=...> they write <a id=... href=...> it is not swaping them, nor when they do <a href=... title=...> , I tried fixing the regular expresion but it was just breaking the code. Any suggestions? thanks Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/#findComment-1088453 Share on other sites More sharing options...
xionhack Posted July 21, 2010 Author Share Posted July 21, 2010 anybody can help me with that regular expression?! Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/#findComment-1089074 Share on other sites More sharing options...
gwolgamott Posted July 21, 2010 Share Posted July 21, 2010 Wow that was a bit more simple then I thought... I stand corrected. Anyways for your regular expression, you could ask in the regex sub-forum. May get a response from experts on that in that forum who are specific in that area. http://www.phpfreaks.com/forums/index.php/board,43.0.html Link to comment https://forums.phpfreaks.com/topic/207499-links-swap/#findComment-1089084 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.