PHP Parse Links/Emails

Php parse links/emails

yes ,
http://www.gidforums.com/t-1816.html

<?php
/**
NAME : autolink()
VERSION : 1.0
AUTHOR : J de Silva
DESCRIPTION : returns VOID; handles converting
URLs into clickable links off a string.
TYPE : functions
======================================*/

function autolink( &$text, $target='_blank', $nofollow=true )
{
// grab anything that looks like a URL...
$urls = _autolink_find_URLS( $text );
if( !empty($urls) ) // i.e. there were some URLS found in the text
{
array_walk( $urls, '_autolink_create_html_tags', array('target'=>$target, 'nofollow'=>$nofollow) );
$text = strtr( $text, $urls );
}
}

function _autolink_find_URLS( $text )
{
// build the patterns
$scheme = '(http:\/\/|https:\/\/)';
$www = 'www\.';
$ip = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$subdomain = '[-a-z0-9_]+\.';
$name = '[a-z][-a-z0-9]+\.';
$tld = '[a-z]+(\.[a-z]{2,2})?';
$the_rest = '\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1}';
$pattern = "$scheme?(?(1)($ip|($subdomain)?$name$tld)|($www$name$tld))$the_rest";

$pattern = '/'.$pattern.'/is';
$c = preg_match_all( $pattern, $text, $m );
unset( $text, $scheme, $www, $ip, $subdomain, $name, $tld, $the_rest, $pattern );
if( $c )
{
return( array_flip($m[0]) );
}
return( array() );
}

function _autolink_create_html_tags( &$value, $key, $other=null )
{
$target = $nofollow = null;
if( is_array($other) )
{
$target = ( $other['target'] ? " target=\"$other[target]\"" : null );
// see: http://www.google.com/googleblog/2005/01/preventing-comment-spam.html
$nofollow = ( $other['nofollow'] ? ' rel="nofollow"' : null );
}
$value = "<a href=\"$key\"$target$nofollow>$key</a>";
}

?>

PHP imap_fetchbody can't get HTML a tag link url

You have to check the multiple sections of the email. You can actually read the source code of an email. It is quite easy to follow and you will identify the different sections.

imap_fetchbody($inbox,$email_number,$section);

http://php.net/manual/en/function.imap-fetchbody.php

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - file.ext

The section you mention on the comments it is a PNG file. Some sections can be encoded in base64 as the email it is a text based protocol.

https://www.base64decode.org/

Example of an email:

 From: Nathaniel Borenstein <nsb@bellcore.com> 
To: Ned Freed <ned@innosoft.com>
Subject: Sample message
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="simple
boundary"

This is the preamble. It is to be ignored, though it
is a handy place for mail composers to include an
explanatory note to non-MIME compliant readers.
--simple boundary

This is implicitly typed plain ASCII text.
It does NOT end with a linebreak.
--simple boundary
Content-type: text/plain; charset=us-ascii

This is explicitly typed plain ASCII text.
It DOES end with a linebreak.

--simple boundary--
This is the epilogue. It is also to be ignored.

https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

Extract link from IMAP message

Some bugs in the code above:

  • Using 1.2 as a floating point number instead of a string. It's a string and it's only sheer luck that your 1.2 is not getting converted as 1.2000000001.
  • Having a hardcoded part number in case the mail comes with multiple body parts -- read the description in RFC 3501, p. 56 for details on what these body parts are, how they work and how to find out which ones are relevant for you
  • Not performing any decoding of the Content-Transfer-Encoding or dealing with character set conversions. You are apparently interested in text parts of a message, these can arrive in multiple encodings like quoted-printable or base64 which you will have to decode. If you'd like to play it safe (you should, it's 2013 already and there are funny characters in URLs, not speaking about the IDNs), also converted from their charset into unicode and only then matched for contents.
  • You should probably check the MIME types of at least all top-level body parts so that you do not try to detect "links" within the attached images or undecoded, binary representaiton of zip files, etc.

parsing multiple urls from the form

Ok, i've fixed it!!!:)

Here is the code:

for ($i=0; $i<count($linki); $i++){

$url = $linki[$i];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result =curl_exec($ch);
curl_close($ch);

preg_match("/[-a-z0-9\._]+@[-a-z0-9\._]+\.[a-z]{2,4}/", $result, $email);//email
print_r($email);

$zapytanie = "INSERT INTO urle set adres = '$email[0]' ";
$wynik = $db->query($zapytanie);

}

Use PHP to Convert Email Text to a Link

  1. str_replace() doesn't use regular expressions, rewritten with preg_replace().
  2. Added delimiters to first matching expression.
  3. Fixed replacement from $1 to $2.

 

function text_filter($string) {
$search = array('/<p>__<\/p>/', '/([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
$replace = array('<hr />', '<a href="mailto:$1">$1</a>');
$processed_string = preg_replace($search, $replace, $string);
echo $processed_string;
}


Related Topics



Leave a reply



Submit