Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7459159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:24:34+00:00 2026-05-29T14:24:34+00:00

I have a php variable with some html content, and some href links. I

  • 0

I have a php variable with some html content, and some href links. I need to capture this links, save to a DB and replace it with the id of the row that I just saved (is to track how many people follows that link in a newsletter application).

Basicatly I need to do the 2 functions in the example (some_function_to_save_the_links_to_array and some_function_to_save_the_links_to_array).

Thank you a lot for your help!

Example:

$var = "<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.google.com'>Some links</a> in the body of this <a href='http://www.yahoo.com'>Newsletter</a> and I want to extract their.</body></html>";
//Here I just no know how to do this, but I need to save http://www.google.com and http://www.yahoo.com to, maybe, an array, and save this array in a mysql db.
some_function_to_save_the_links_to_array;
while (THERE ARE VALUES IN THE ARRAY OF THE LINKS){
 save $array['X'] to db //(I already know how to do it, this is not the problem)
 $id = last inserted row in db //I know how to do it also
 function_to_replace_the_links_for_the_id;
}
echo $var;

And this is the echo:
<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.mysite.com/link.php?id=1'>Some links</a> in the body of this <a href='http://www.mysite.com/link.php?id=1'>Newsletter</a> and I want to extract their.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:24:35+00:00Added an answer on May 29, 2026 at 2:24 pm
    <?php
    function captureLink($content) 
    {
        $links = array();
        $pattern = "/<a\s+href=[\"\']([^>]+?)[\"\']/iU";
        if(preg_match_all($pattern,$content,$matches)) {
            for($i = 0;$link = $matches[$i][1];$i++)
                array_push($links,$link);
        }
        return $links;
    }
    
    function insertLinksToDb(array $links)
    {
        $linksDb = array();
        foreach($links as $link) {
            $hash_link = md5($link);
            $sql = "SELECT (id) FROM links WHERE hash LIKE :hash";
            $sth = $dbh->prepare($sql);
            $sth->bindValue(':hash',$hash_link,PDO::PARAM_STR);
            $sth->execute();
            $result = $sth->fetch(PDO::FETCH_ASSOC);
            if(!empty($result)) {
                $id = $result['id'];
                $linksDb[$id] = $link;
            } else {
                $sql = " INSERT INTO links (hash,link) VALUES(:hash,:link);";
                $sth = $dbh->prepare($sql);
                $sth->execute(array(':hash'=>$hash_link,':link',$link));
                $linksDb[PDO::lastInsertId] = $link;
            }
        }
        return $linksDb;
    }
    
    function normallizeLinks($content,array $links)
    {
        foreach($links as $id => $link) {
            //str_replace working faster than strtr
            $content = str_replace($link,'/links.php?id='.$id,$content);
        }
        return $content;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html form and some php that input the POST variables
I am putting a whole HTML content inside a PHP variable and inside that
I have a PHP-variable called $go_Adress which contains the adress I need to get
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have some HTML/PHP code in Dreamweaver and for the life of me I
I have 2 files that are applicable to this question, myaccount.php and test5.php .
I have the following code generating content on my site: <div class=content id=links> <?php
I have a settings.php file with parametric variables and a functions.php with well, some
I have a multiple language website, and I use a php get variable to
I have the following string in the smarty (php templating system) variable $test: <img

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.