Reader
  • Home
  • About
  • Deals
  • Services
  • Contact
You are here: Home / Archives for extract

How to extract username from an e-mail address string

Last updated on September 7th, 2008 by Gabriel Livan

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 = '[email protected]';

$username = getUsernameFromEmail($email);

echo $username; // thecoder
?>

Filed Under: PHP Tagged With: e-mail, extract

How to extract domain name from an e-mail address string

Last updated on February 27th, 2015 by Gabriel Livan

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 = '[email protected]';

$domain = getDomainFromEmail($email);

echo $domain; // yahoo.com
?>

Filed Under: PHP Tagged With: e-mail, extract

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

Last updated on July 9th, 2009 by Gabriel Livan

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;
}
?>

Filed Under: PHP Tagged With: extract

How to extract images from an URL in PHP

Last updated on July 9th, 2009 by Gabriel Livan

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.

Filed Under: PHP Tagged With: extract

How to extract content between two delimiters in PHP

Last updated on March 21st, 2015 by Gabriel Livan

Here’s a function which is useful when you need to extract some content between two delimiters. For instance you need to extract content using a robot that connects to a page.

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/extract-content-between-two-delimiters-with-php.html
*/

function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);

$str = substr($string, $pos);

$str_two = substr($str, strlen($start));

$second_pos = stripos($str_two, $end);

$str_three = substr($str_two, 0, $second_pos);

$unit = trim($str_three); // remove whitespaces

return $unit;
}

Read more from this entry…

Filed Under: PHP Tagged With: delimiters, extract

« Previous Page

About Me

Gabriel LivanHey, there! I'm Gabriel Livan, Web Developer, Blogger and Internet Marketer. I love making fast, secure and scalable web applications, running and travelling around the world. I blog about WordPress, site speed & security and quality tools for web professionals. Read more

What are you looking for?

Categories

  • AJAX (22)
  • Deals (10)
  • Design (30)
  • eBooks (2)
  • Flash (19)
  • Freebies (11)
  • General (62)
  • Giveaways (1)
  • Graphics (27)
  • HTML & CSS (35)
    • Frameworks (9)
  • JavaScript (160)
    • Frameworks (8)
    • jQuery (89)
    • MooTools (14)
  • Page Loading Speed (2)
  • Photoshop (3)
  • PHP (113)
  • Reviews (8)
  • SEO (6)
  • Statistics (2)
  • Web Tools (13)
  • WordPress (27)
    • Plugins (15)
    • Snippets (4)
    • Tools (2)
    • Tutorials (6)

Copyright © 2026 · BitRepository.com - All Rights Reserved

  • Home
  • About
  • Contact