Jump to content

Links swap


xionhack

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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