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
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
PHP / MySQL Associative Multidimensional Array:
Hello.
I have data in a MySQL Table that adheres to the below: (note, no index, could add if
Login Functionality Working Different on IE
This is an odd one, I have a site which has an admin section. The admin pages unsurprisingly require
i want run some web server under the ip sharer
hello i want run some web server under the ip sharer
my ip is floating ip but it doesnt matter
I need to increse 6hours more, and i don't know how???
I need to increse 6hours more, and i don't know how???
<tr>
<t
mail() problem
Hi Guys,
I've set up a contact email form. It sends the information fine, but it sends it to
How would I do "Users not logged in since *** delete them"
I know how I would set this up, and delete the user roughly.
I'll create a table called "Use
Undefined Index Notice In Internet Explorer Only
I am getting the following notice:
QuoteNotice: Undefined index: jrox in /home/ycsn/public_h
PHP/Database issue
My friend is helping me make a database where you go to a certain webpage of my site and the page wi
Script not running properly
Hello Everyone
This is a basic script for posting to a database. I'm not sure why it doesn't work