Jump to content

Swaping links


xionhack

Recommended Posts

Hello. I have a questions that I think its pretty much about regular expresions. I have this code:

 

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;
}

 

What that does, is that it checks for all the links in a page, saves the links in a database and then swap the link address with the id of that link in the database. The problem that i have is that the code works when the link is just "<a href="" ></a>" but if the <a> tag has an attribute, then it wont work. i.e <a href="" id=""></a> OR <a id="" title="" href=""></a>

Link to comment
Share on other sites

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' . $matches[1].'href="' . mysql_insert_id() .'"' . $matches[3] . '>' . $matches[4] .'</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! it works well! except for something. It doesnt want to take some links that are long like :

 

http://www.facebook.com/album.php?aid=8228&id=680439301#!/pages/test/test-test/162482691457?v=wall

 

Also, I dont want to get the mailto: tags but just leave them as mailto: . I was trying the following regular expression and code but its not working either, its not taking the attribute tags nor the mailto, I find regex so complicated!:

 

$pattern = '%<a (??!href)[^>])*+href\s*+=\s*+"([^"]+)"[^>]*+>([^<]*+(??!</a>)<[^<]*+)*+)</a>%';

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.