Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMasterBlog > Site Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Extract URLs

During the last week, two projects needed URLs extracted (selected and copied, not removed) from within text content. One was for a client project, the other was for the new Pro URL (still in development).

I'll show you how I did it. Perhaps you have, or will have, a project where you need to extract the URLs.

The code is PHP. The PHP script will find URLs from within plain text, which may include text that is marked up with HTML tags. Text content copied straight from the browser window of a web page and pasted into the text box should extract all absolute HTTP and HTTPS URLs from within the pasted text, including URLs of linked text. Relative URLs won't be found, only absolute URLs beginning with http and https.

The text with URLs is pasted into a text box — not a textarea box but an editable div box. An editable div can hold source code in ways a textarea field can not.

After the content with URLs is pasted into the text box, click the button to extract the URLs and list them in your browser window.

The software is all in one PHP file. No customization is required. Simply upload it to your server and use it.

Here is a screenshot with the text box. When using the live software and tapping the button, the extracted URLs are listed below the text box.

Click here for a live implementation.

If you like what it does and want the functionality on your website, here is the source code.

<?php
// URL Extractor
// April 14, 2023
// Will Bontrager Software LLC
if( isset($_POST['extractURLs']) )
{ // Extracts any http... URLs and returns them to browser.
   $matches = array();
   preg_match_all('!https?://[^"\'\s<>]+!',$_POST['extractText'],$matches);
   if( empty($matches[0]) ) { echo 'No URLs found.'; }
   else { echo implode("\n",$matches[0]); }
   exit;
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Extract URLs</title>
<style>body { font-size:100%; }</style>
</head>
<body><div style="max-width:500px; margin:.5in auto;">

<h1 class="interior-page-header">Extract URLs</h1>

<div id="text-container" style="border:2px solid #ccc; border-radius:.5em; padding:.5em; min-height:1in; margin:1em 0;" contenteditable="true"></div>
<input type="button" onclick="SubmitFormWithTextToExtract()" style="width:100%;" value="Extract URLs">

<div id="url-container" style="border:1px dotted #ccc; border-radius:.5em; padding:.5em; min-height:.2in; margin:1em 0; white-space:pre; font-family:monospace; overflow:auto;"></div>

<script type="text/javascript">
function SubmitFormWithTextToExtract()
{ // Submits text with URLs to the PHP code at the top of this page and posts the response.
   document.getElementById("url-container").innerHTML = "";
   let http = new XMLHttpRequest();;
   if(! http) { alert("Sorry, unable to connect to the internet. Perhaps tapping again will get through."); return; }
   var params = new Array();
   params.push( "extractURLs=yes" );
   params.push( "extractText=" + encodeURIComponent(document.getElementById("text-container").innerHTML) );
   http.onreadystatechange = function()
   {
      if(http.readyState == 4)
      {
         if(http.status == 200) { document.getElementById("url-container").innerHTML = http.responseText; }
         else { alert('Content request error, status code: '+http.status+' '+http.statusText); }
      }
   }
   http.open("POST","<?php echo($_SERVER['PHP_SELF']) ?>",true);
   http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http.send( params.join("&") );
} // function SubmitFormWithTextToExtract();
</script>

</div></body>
</html>

Save the source code as extractURLs.php or other suitable *.php file name. Upload it to your server. To use the software, types its URL into your browser.

URLs beginning with http:// or https:// will be extracted and listed. URLs may be to web pages, images, or elsewhere. If a URL is found more than once, it will be listed more than once.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Keeping Image Location Secret

The URL of an image embedded in a web page may be kept secret.

Easier Reading of JSON Data

For easier reading of JSON data, convert the JSON into an array.

Fixed Position Image

Position an image within a browser window that won't move even with page scroll.

Visually Centering Images

Sometimes an image that is technically centered doesn't look quite centered when viewed.

Cookie Directory Protection

Protecting subdirectories with a cookie can be an especially good method when access needs to be allowed from various internet connections.

Check SSL Certificate

An easy-to-use SSL checker to see when your secure certificate expires.

Strong Form Protection From Bots

A web page form that is invisible to spam bots.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC