/    Sign up×
Community /Pin to ProfileBookmark

ereg — Regular expression match

I want to detect all the mail adresses on a webpage and convet them on mailto/Hyperlinks and this is what I have until now, someone can give me hand please ?

_Lobo_

[url]http://ch2.php.net/ereg[/url]

[code=php]<?php

function detectEmail($email)
{
return ereg(“^[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]+$”, $email);

echo $email(“<a href=’mailto:”^[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]+$”‘>””</a>”);}

?>[/code]

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@DJsACNov 18.2004 — finally figured it out ?
[code=php]<?php
$text = "Hello, this is a test to [email][email protected][/email] see if it changes all 3 of the emails. [email][email protected][/email] It also works if the same address comes along more than once.an [email][email protected][/email] though i'm not sure about what it does to <a href='mailto:[email protected]'>[email protected]</a> :-) n";
$email = $text;

// the callback function
function add_link($matches)
{
//print_r($matches); // show's array's structure
return '<a href="mailto:'.$matches[0].'">'.$matches[0].'</a>';
}

echo preg_replace_callback("/[a-z0-9]+([-_.]?[a-z0-9])+@[a-z0-9]+([-_.]?[a-z0-9])+.[a-z]{2,4}/","add_link",$text);
?>[/code]
Copy linkTweet thisAlerts:
@_LOBO_authorNov 18.2004 — Thank you DJsAC but I think Im doing something wrong coz my code dont work anymore ?


[code=php]<head>
<style type="text/css">
table {
font-family: arial, sans-serif;
font-size: smaller;
border-collapse: collapse;
border: solid black thin;
width: 500px;
}
th,td {
margin: 0;
padding: 3px;
border: solid black thin;
}
th {
font-weight: bold;
text-align: center;
}
</style>
</head>

<body>
<?php


$email = $value;//Code added

$handle = fopen("test.csv", "r");
$data = fgetcsv($handle, 1000, ";");

# column headers from 1st row of CSV:
echo "<tr>"; // removed style attribute as $bg not defined here
foreach($data as $value)
{
echo "<th>$value</th>";
}
echo "</tr>n";

# data rows:
$thisRow = 1;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
if($thisRow++ % 2 == 0) // $row not defined, $thisRow is though
{
$bg = "#CCCCCC"; // added $ and ;
}
else
{
$bg = "#FFFFFF";
}
echo "<tr style='background-color: $bg'>"; // added style attribute, assumed it was meant to be here
foreach($data as $value)
{
echo "<td>$value</td>";
}
echo "</tr>n";
}
echo "</table>n";

//code added

// the callback function
function add_link($matches)
{
//print_r($matches); // show's array's structure
return '<a href="mailto:'.$matches[0].'">'.$matches[0].'</a>';
}

echo preg_replace_callback("/[a-z0-9]+([-_.]?[a-z0-9])+@[a-z0-9]+([-_.]?[a-z0-9])+.[a-z]{2,4}/","add_link",$value);
echo "<table>";


}}


?>
</body>
</html> [/code]
Copy linkTweet thisAlerts:
@DJsACNov 18.2004 — From what I gather from your code it works as follows:

(/*- comments are mine */)
[code=php]<?php


$email = $value;//Code added
/* this variable is not necessary */
$handle = fopen("test.csv", "r");
$data = fgetcsv($handle, 1000, ";");
/*Open file and save contents in array $data */
# column headers from 1st row of CSV:
/* You forgot to "START" the <table> */
echo "<tr>"; // removed style attribute as $bg not defined here
foreach($data as $value)
{
echo "<th>$value</th>";
/* echo column headers */
}
echo "</tr>n";

# data rows:
$thisRow = 1;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
if($thisRow++ % 2 == 0) // $row not defined, $thisRow is though
{
$bg = "#CCCCCC"; // added $ and ;
}
else
{
$bg = "#FFFFFF";
}
echo "<tr style='background-color: $bg'>"; // added style attribute, assumed it was meant to be here
foreach($data as $value)
{
echo "<td>$value</td>";
/*echo values in .csv as are, without changes. */
}
echo "</tr>n";
}
echo "</table>n";

//code added

// the callback function
function add_link($matches)
{
//print_r($matches); // show's array's structure
return '<a href="mailto:'.$matches[0].'">'.$matches[0].'</a>';
}
/* It's probably better if you put this code near the top of the page. */

echo preg_replace_callback("/[a-z0-9]+([-_.]?[a-z0-9])+@[a-z0-9]+([-_.]?[a-z0-9])+.[a-z]{2,4}/","add_link",$value);
/* This outputs the $value with all the links 'fixed', I'm guessing this should be in the place of the «echo "<td>$value</td>";» instead. */
echo "<table>";
/* Start table? */

}}


?>
</body>
</html> [/code]
Copy linkTweet thisAlerts:
@DJsACNov 18.2004 — [i]Originally posted by _LOBO_ [/i]

[B]don't work sorry, take a look

http://www.nhfg-intl.com/christian/csv/csv.php [/B]
[/QUOTE]
How can that page be completely blank? (including source)

If you have CSS code at the top that should at least appear on the source code...

And so should the echo's "<tr>" etc...

strange... ?

can you post an example of the .csv file? So I can see what kind of data it's collecting? (How it's organized?)
Copy linkTweet thisAlerts:
@_LOBO_authorNov 19.2004 — ok this is what is inside:

[code=php]

ID;Nachname;Vorname;Ort;Land;EmailAdr
727349;Genbach;Andreas;Aachen;Deutschland;[email protected]
728283;Deleon;Giambattista;Paderborn;Deutschland;
729148;Chris;Ulrich;Rosengarten;Deutschland;[email protected]

[/code]
Copy linkTweet thisAlerts:
@DJsACNov 19.2004 — if you make sure each row has 6 ';' in it, you can split $value into a

list($id,$lastname,$firstname,$city,$country,$email) = explode(";",$value);

and then $emailaddress = "<a href='mailto:".$email."'>Mail: ".$email."</a>";

echo $emailaddress shows complete Html mailto text?

No more need to search all the values in the csv files.
Copy linkTweet thisAlerts:
@_LOBO_authorNov 19.2004 — Ok I foudn this function called ereg_replace that can help, but can you show me how to implement this script?

(PHP 3, PHP 4 , PHP 5)

ereg_replace -- Replace regular expression

Description

string ereg_replace ( string pattern, string replacement, string string)

This function scans string for matches to pattern, then replaces the matched text with replacement.

The modified string is returned. (Which may mean that the original string is returned if there are no matches to be replaced.)

If pattern contains parenthesized substrings, replacement may contain substrings of the form digit, which will be replaced by the text matching the digit'th parenthesized substring; 0 will produce the entire contents of string. Up to nine substrings may be used. Parentheses may be nested, in which case they are counted by the opening parenthesis.

If no matches are found in string, then string will be returned unchanged.


[COLOR=orangered]=====This will activate URL=====sample that I found[/COLOR]

[code=php]
function html_activate_urls($str)
{
if(!preg_match("http://", $str)) {
$str = "http://$str";
}
// lift all links, images and image maps
preg_match_all("/<a [^>]+>.*</a>/is", $str, $matches, PREG_SET_ORDER);
foreach($matches as $match)
{
$key = "<" . md5($match[0]) . ">";
$search[] = $key;
$replace[] = $match[0];


preg_match_all("/<map [^>]+>.*</map>/is", $str, $matches, PREG_SET_ORDER);
foreach($matches as $match)
{
$key = "<" . md5($match[0]) . ">";
$search[] = $key;
$replace[] = $match[0];
}

preg_match_all("/<img [^>]+>/is", $str, $matches, PREG_SET_ORDER);
foreach($matches as $match)
{
$key = "<" . md5($match[0]) . ">";
$search[] = $key;
$replace[] = $match[0];
}

$str = str_replace($replace, $search, $str);

// indicate where urls end if they have these trailing special chars
$sentinals = array("'&(quot|#34);'i", // Replace html entities
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(d+);'e");
$str = preg_replace($sentinals, "^^sentinal^^\0^^sentinal^^", $str);

$vdom = "[:alnum:]"; // Valid domain chars
$vurl = $vdom."_~-"; // Valid subdomain and path chars
//$vura = "À-ßà-ÿ!#$%&*+,;=@.".$vurl; // Valid additional parameters (after '?') chars;
$vura = "À-ßà-ÿ!#$%&*+,;=@./".$vurl; // Valid additional parameters (after '?') chars;
// insert other local characters if needed
$protocol = "[[:alpha:]]{3,10}://"; // Protocol exp
$server = "([$vurl]+[.])*[$vdom]+"; // Server name exp
$path = "(([$vurl]+([.][$vurl]+)*/)|([.]{1,2}/))*"; // Document path exp (/.../)
$name = "[$vurl]+([.][$vurl]+)*"; // Document name exp
$params = "[?][$vura]*"; // Additional parameters (for GET)

$str = eregi_replace("$protocol$server(/$path($name)?)?($params)?", "<a href="\0">\0</a>", $str); // URL into links

$str = str_replace("^^sentinal^^", '', $str);
return str_replace($search, $replace, $str);
}
[/code]
Copy linkTweet thisAlerts:
@DJsACNov 19.2004 — This will work ?

[code=php]<?php
echo "<table>";
echo "<tr>"; // removed style attribute as $bg not defined here
# data rows:
$handle = fopen("data.csv", "r");
$data = fgetcsv($handle, 1000, ";");
foreach($data as $value)
{
echo "<th>$value</th>";
}
$thisRow = 1;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
if($thisRow++ % 2 == 0) // $row not defined, $thisRow is though
{
$bg = "#CCCCCC"; // added $ and ;
}
else
{
$bg = "#FFFFFF";
}
echo "<tr style='background-color: $bg'>"; // added style attribute, assumed it was meant to be here
print_r($data);
$data[5] = "<a href='mailto:".$data[5]."'>".$data[5]."</a>";
foreach($data as $value)
{
echo "<td>$value</td>";
}
echo "</tr>n";
}
echo "</table>n";



?> [/code]
Copy linkTweet thisAlerts:
@_LOBO_authorNov 19.2004 — dont't work, and IE/Firefox get frezze + my CPU jump to 100% and at the end I get a huge black box on the html page.

http://www.nhfg-intl.com/christian/csv/csv.php ? ?

on the html I get this

[code=php]
head>
<style type="text/css">
table {
font-family: arial, sans-serif;
font-size: smaller;
border-collapse: collapse;
border: solid black thin;
width: 500px;
}
th,td {
margin: 0;
padding: 3px;
border: solid black thin;
}
th {
font-weight: bold;
text-align: center;
}
</style>
</head>

<body>
<table><tr><tr style='background-color: #FFFFFF'><td><a href='mailto:'></a></td></tr>
<tr style='background-color: #CCCCCC'><td><a href='mailto:'></a></td></tr>
<tr style='background-color: #FFFFFF'><td><a href='mailto:'></a></td></tr>
<tr style='background-color: #CCCCCC'><td><a href='mailto:'></a></td></tr>

[/code]



more of 3500 lines of this

<tr style='background-color: #CCCCCC'><td><a href='mailto:'></a></td></tr>
Copy linkTweet thisAlerts:
@DJsACNov 19.2004 — The script works on my servers:

http://s87274285.onlinehome.us/data.php

data.php[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>data page php</title>
<style type="text/css">
table {
font-family: arial, sans-serif;
font-size: smaller;
border-collapse: collapse;
border: solid black thin;
width: 500px;
}
th,td {
margin: 0;
padding: 3px;
border: solid black thin;
}
th {
font-weight: bold;
text-align: center;
}
</style>
</head>

<body>
<?php
echo "<table>";
echo "<tr>"; // removed style attribute as $bg not defined here
# data rows:
$handle = fopen("data.csv", "r");
$data = fgetcsv($handle, 1000, ";");
foreach($data as $value)
{
echo "<th>$value</th>";
}
$thisRow = 1;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
if($thisRow++ % 2 == 0) // $row not defined, $thisRow is though
{
$bg = "#CCCCCC"; // added $ and ;
}
else
{
$bg = "#FFFFFF";
}
echo "<tr style='background-color: $bg'>"; // added style attribute, assumed it was meant to be here
//print_r($data);
$data[5] = "<a href='mailto:".$data[5]."'>".$data[5]."</a>";
foreach($data as $value)
{
echo "<td>$value</td>";
}
echo "</tr>n";
}
echo "</table>n";



?>
</body>
</html> [/code]


data.csv
[CODE]ID;Nachname;Vorname;Ort;Land;EmailAdress
727349;Genbach;Andreas;Aachen;Deutschland;[email protected]
728283;Deleon;Giambattista;Paderborn;Deutschland;
729148;Chris;Ulrich;Rosengarten;Deutschland;[email protected][/CODE]
Copy linkTweet thisAlerts:
@_LOBO_authorNov 19.2004 — sorry, It works now, it was my misstake, THANK YOU !!!!!!!?

is a great script thanX to you, hehehehehe

now explain me why you put $data[5] if the row of the mail is 6 ?
Copy linkTweet thisAlerts:
@DJsACNov 19.2004 — array's start with 0 ?
ID;Nachname;Vorname;Ort;Land;EmailAdress

$data[0] = ID

$data[1] = Nachname

$data[2] = Vorname

$data[3] = Ort

$data[4] = Land

$data[5] = EmailAdress ?
Copy linkTweet thisAlerts:
@_LOBO_authorNov 19.2004 — cool is good to know ? now Iamde a detection of www, and works great thak you very much for your help
Copy linkTweet thisAlerts:
@DJsACNov 19.2004 — no problem ?

I like a nice challenge every now and then ?
×

Success!

Help @_LOBO_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...