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 your1.2
is not getting converted as1.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 likequoted-printable
orbase64
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
str_replace()
doesn't use regular expressions, rewritten withpreg_replace()
.- Added delimiters to first matching expression.
- 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
Clear Data in MySQL Table with PHP
Soapclient: How to Pass Multiple Elements with Same Name
Error 1366 (Hy000): Incorrect String Value: '\Xf0\X9F\X98\X9C' for Column 'Comment' at Row 1
Http Auth via PHP - PHP_Auth_User Not Set
What Is the Rfc Compliant and Working Regular Expression to Check If a String Is a Valid Url
Php: Check If Xml Node Exists with Attribute
Why Is Turning Magic_Quotes_Gpc on Considered a Bad Practice
Tcpdf and Insert an Image Base64 Encoded
Destination Path for Move_Uploaded_File in PHP
Zend Framework 2 - Removed Form Element Causes Validation to Fail
Hightest Value of an Associative Array
Get Request from PHP Using File_Get_Contents with Parameters
PHP Domdocument - Get HTML Source of Body
How to Get/Set Session_Id() or Should It Be Generated Automatically
PHP MySQLi Commands Out of Sync; You Can't Run This Command Now