Find only certain URLs from page ... regex (semi-complete script)
Posted on
16th Feb 2014 07:03 pm by
admin
Hi guys,
What I need to do is take a page & extract all the URLs from the page & place them in an array.
Did you know?Explore Trending and Topic pages for more stories like this.
However I only need to grab certain URLS
eg.
site1.com
site1.com/folder/thisfile.zip
site2.com
site2.com/some/folder/or/subfolder/1.mp3
site3.com
but then leave out of the array
site4.com
site5.com/the/script/needs/to/be/able/to/grab/sub/folders/and/files/2.mp3
Here's the script I've got so far but this will grab ALL the links ... so I need to modify this & perhaps use an if or switch statement to check whether it's a link I actually want...
<?php
$string = '<a href="http://www.example.com">Example.com</a> has many links with
examples <a href="http://www.example.net/file.php">links</a> to many sites and
even urls without links like http://www.example.org just to fill the gaps and
not to forget this one http://phpro.org/tutorials/Introduction-to-PHP-Regex.html
which has a space after it. The script has been modifiied from its original so now
it grabs ssl such as https://www.example.com/file.php also';
/**
*
* @get URLs from string (string maybe a url)
*
* @param string $string
*
* @return array
*
*/
function getUrls($string)
{
$regex = '/https?://[^" ]+/i';
preg_match_all($regex, $string, $matches);
return ($matches[0]);
}
$urls = getUrls($string);
foreach($urls as $url)
{
echo $url.'<br />';
}
?>
No comments posted yet
Your Answer:
Login to answer
339
23
Other forums
Setting/Modify Headers using mail() function
I have a question about setting email headers when using mail() in a php script.
When I send
Security Exception on pages using AJAX
I am getting the exception: attempted to perform an operation not allowed by the security policy on
JSON SORT WITH PHP
I have two products that I want to sort by say "Id:17, value: xxx" using php
The page w
Add code to enable passing of checkbox variable on login
Ok, I have a login page, but want to add a checkbox that when checked and successful login is made i
add text and number image
hi guys ..
ok see i want the user to add their name and pick a number then click get it and i
Display a default image
I am trying to display generic image for items that don't have one, but I can't get it to display.
RTF fomatting to email content
Im trying to sen an email with content is picked up from a rtf-file (file_get_contents('*.rtf'). Mai
I face problems to extract data from one table and insert it in another one
Hi,
For modifications, I have to extract data from one table and insert it in another one
Add 5 to a variable when a button is clicked, and re-run a for loop
So I'm making a feedback sort of section on a website with MySQL and PHP, I've gotten the script to
insert Multiple rows into the table from that table data
Hi All,
I have a requirement like to insert mulitple rows into the table from that