» Birthday Bundle - Over $400 worth of Envato files for just $20
  Posts tagged 'extract'

PHP: Match Non-Alphanumeric Characters from a String

Posted on October 5, 2008, Filled under PHP,  Bookmark it

This is a snippet that matches non-alphanumeric characters from a string (text).

<?php
function match_non_alphanumeric_characters($string, $strip_space = true) // Match spaces?
{
$str = ($strip_space) ? ' ' : '';
preg_match_all('/([^a-zA-Z0-9'.$str.']+)/', $string, $match);

return $match[0];
}

$string = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent porttitor euismod enim # Sed posuere & Duis non elit * Sed tempus dolor$';

$non_alphanumeric_array = match_non_alphanumeric_characters($string);

echo '<pre>'; print_r($non_alphanumeric_array); echo "</pre>";
?>

Output:

Array
(
    [0] => ,
    [1] => .
    [2] => #
    [3] => &
    [4] => *
    [5] => $
)

PHP: Extract Alphanumeric Sequences from a String

Posted on October 5, 2008, Filled under PHP,  Bookmark it

This short snippet is useful if you need to extract alphanumeric sequences (characters) from a string:

function extract_alphanumeric_sequences($string)
{
preg_match_all('/([a-zA-Z0-9]+)/', $string, $match);

return $match[0];
}

$string = 'Suspendisse 354 65pretium eros ut 43564 mauris. Integer in lacus quis est dignissim posuere. Aenean dui.  &$#^@ %$ 94375  Mauris non turpis sit amet nunc imperdiet varius.';

$alpha_array = extract_alphanumeric_sequences($string);

echo '<pre>'; print_r($alpha_array); echo "</pre>";

Output:

Array
(
    [0] => Suspendisse
    [1] => 354
    [2] => 65pretium
    [3] => eros
    [4] => ut
    [5] => 43564
    [6] => mauris
    [7] => Integer
    [8] => in
    [9] => lacus
    [10] => quis
    [11] => est
    [12] => dignissim
    [13] => posuere
    [14] => Aenean
    [15] => dui
    [16] => 94375
    [17] => Mauris
    [18] => non
    [19] => turpis
    [20] => sit
    [21] => amet
    [22] => nunc
    [23] => imperdiet
    [24] => varius
)

PHP: Extract Alphabetical Sequences from a String

Posted on October 5, 2008, Filled under PHP,  Bookmark it

This function extracts alphabetical sequences (usually words) from a string (text):

function extract_alphabetical_sequences($string)
{
preg_match_all('/([a-zA-Z]+)/', $string, $match);

return $match[0];
}

$string = 'In hac habitasse platea dictumst. 54 8*5 5#$&*^@  $ @ In eget sem. Etiam quam. Nam nec, justo sed lorem, auctor tincidunt. Nulla id risus laoreet nisl  *^$&&@#$ egestas ultrices.';

$alpha_array = extract_alphabetical_sequences($string);

echo '<pre>'; print_r($alpha_array); echo "</pre>";

Output:

Array
(
    [0] => In
    [1] => hac
    [2] => habitasse
    [3] => platea
    [4] => dictumst
    [5] => In
    [6] => eget
    [7] => sem
    [8] => Etiam
    [9] => quam
    [10] => Nam
    [11] => nec
    [12] => justo
    [13] => sed
    [14] => lorem
    [15] => auctor
    [16] => tincidunt
    [17] => Nulla
    [18] => id
    [19] => risus
    [20] => laoreet
    [21] => nisl
    [22] => egestas
    [23] => ultrices
)

If you need to extract all the words from a string, consider using the str_word_count() function.

PHP: How to extract numbers from a string (text)

Posted on October 5, 2008, Filled under PHP,  Bookmark it

This is a short function that extracts numbers from a string:

function extract_numbers($string)
{
preg_match_all('/([\d]+)/', $string, $match);

return $match[0];
}

$string = 'Lorem ipsum dolor sit 45 40 amet, consectetuer adipiscing elit. 35 65675 Suspendisse sed nibh non diam consectetuer pharetra. Morbi ultricies 235 536pede et pede. 9432 3536 Nunc eu risus eget quam lacinia feugiat. In sapien sem, fringilla quis, 34 24 8762condimentum id, bibendum ut, nibh. Quisque 2367 784 elementum massa 350 235 vel nulla.';

$numbers_array = extract_numbers($string);

echo '<pre>'; print_r($numbers_array); echo "</pre>";

Output:

Array
(
    [0] => 45
    [1] => 40
    [2] => 35
    [3] => 65675
    [4] => 235
    [5] => 536
    [6] => 9432
    [7] => 3536
    [8] => 34
    [9] => 24
    [10] => 8762
    [11] => 2367
    [12] => 784
    [13] => 350
    [14] => 235
)

PHP: Get Main Base URL

Posted on September 8, 2008, Filled under PHP,  Bookmark it

This is a snippet which extracts the Main Base Address from a complete URL:

<?php
function GetMainBaseFromURL($url)
{
$chars = preg_split('//', $url, -1, PREG_SPLIT_NO_EMPTY);

$slash = 3; // 3rd slash

$i = 0;

foreach($chars as $key => $char)
{
	if($char == '/')
	{
	   $j = $i++;
	}

	if($i == 3)
	{
	   $pos = $key; break;
	}
}

$main_base = substr($url, 0, $pos);

return $main_base.'/';
}

$url = 'http://mysubdomain.mydomain.com/category/hardware/subcategory/mainboards.html';

$main_base = GetMainBaseFromURL($url);

// Output: http://mysubdomain.mydomain.com/

echo $main_base;
?>

How to extract username from an e-mail address string

Posted on September 5, 2008, Filled under PHP,  Bookmark it

If you need to extract the username from an e-mail address string then this snippet can help you. For instance, you can use this function inside a loop, while selecting emails from a database.

<?php
function getUsernameFromEmail($email)
{
$find = '@';
$pos = strpos($email, $find);

$username = substr($email, 0, $pos);

return $username;
}

$email = 'thecoder@domain.com';

$username = getUsernameFromEmail($email);

echo $username; // thecoder
?>

If, for any reason, you need to extract the domain from an e-mail address (or from multiple e-mail address that are in a database) then you can use this function:

<?php
function getDomainFromEmail($email)
{
// Get the data after the @ sign
$domain = substr(strrchr($email, "@"), 1);

return $domain;
}

// Example

$email = 'the_username_here@yahoo.com';

$domain = getDomainFromEmail($email);

echo $domain; // yahoo.com
?>

Extract URL(s) from Link(s) with PHP

Posted on September 4, 2008, Filled under PHP,  Bookmark it

This is a script which extracts URLs from Links. The function gets the content from the HREF attribute and ignores the non-urls like: “javascript: openWindow()”.

Using Regular Expressions

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

$url = 'http://www.php.net/';

// Fetch page
$string = FetchPage($url);

// Regex that extracts the urls from links

$links_regex = '/<a[^/>]*'.

'href=[\"|\']([^javascript:].*)[\"|\']/Ui';

preg_match_all($links_regex, $string, $out, PREG_PATTERN_ORDER);

echo "<pre>"; print_r($out); echo "</pre>";

function FetchPage($path)
{
$file = fopen($path, "r"); 

if (!$file)
{
exit("The was a connection error!");
} 

$data = '';

while (!feof($file))
{
// Extract the data from the file / url

$data .= fgets($file, 1024);
}
return $data;
}
?>

How to extract images from an URL in PHP

Posted on August 30, 2008, Filled under PHP,  Bookmark it

Here’s a piece of code that extracts images from an URL. This only works if the images are displayed using the IMG tag (not CSS).

This script only shows images. It doesn’t download any. The src attribute must have a full url in order for the images to show.

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/

$url = 'http://www.microsoft.com/';

// Fetch page
$string = FetchPage($url);

// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex, $string, $out, PREG_PATTERN_ORDER);

$img_tag_array = $out[0];

echo "<pre>"; print_r($img_tag_array); echo "</pre>";

// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.

'src=[\"|\'](.*)[\"|\']/Ui';

preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);

$images_url_array = $out[1];

echo "<pre>"; print_r($images_url_array); echo "</pre>";

// Fetch Page Function

function FetchPage($path)
{
$file = fopen($path, "r"); 

if (!$file)
{
exit("The was a connection error!");
} 

$data = '';

while (!feof($file))
{
// Extract the data from the file / url

$data .= fgets($file, 1024);
}
return $data;
}
?>

NOTE: You can use this script to extract images from local files too (not necessarily URLs).

Feel free to post any comments or suggestions regarding this script.