By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

A problem with '"'

P: n/a
I have written a code and can't figure what is wrong, the code will
read a content of a file and cut all the emails from it,then echo them,
but I have a problem with '"' charecter I think..
Take a look:
****************************
<?php
$url="http://www.bihstudenti.com/kontakt.php";
$lines = file_get_contents($url);
$done=explode("@",$lines);
for($i=0;$i<count($done)+1;$i++)
{
$j=$i+1;
$a=strrpos($done[$i],":");
$b=strrpos($done[$i],">");
$c=strpos($done[$j],chr(34));
$d=strpos($done[$j],"<");
$front=$done[$i];
$back=$done[$j];
if($a>$b){
if($c<$d){
echo "Line $i
".substr($front,strrpos($front,":")+1)."@".substr( $back,0,strpos($back,chr(34)))."<br>";
}else{
echo "Line $i
".substr($front,strrpos($front,":")+1)."@".substr( $back,0,strpos($back,"<"))."<br>";
}
}else{
if($c<$d){
echo "Line $i
".substr($front,strrpos($front,">")+1)."@".substr( $back,0,strpos($back,chr(34)))."<br>";
}else{
echo "Line $i
".substr($front,strrpos($front,">")+1)."@".substr( $back,0,strpos($back,"<"))."<br>";
}
}
}
?>
****************************
I am looking at www.bihstudenti.com/kontakt.php content..
Please help, do any one have a clue what is wrong..

Oct 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
NurAzije wrote:
read a content of a file and cut all the emails from it,then echo them,


Sorry, can't be bothered to check the code :) but try this:

$data = file_get_contents('http://www.bihstudenti.com/kontakt.php');
$match = array();
preg_match_all('/mailto:([^"]+)">/', $data, $match);
print_r($match[1]);

See http://php.net/preg-match-all

--
E. Dronkert
Oct 22 '05 #2

P: n/a
It have succeded for the after mailto: ones, thank you very much, I am
a beginner in PHP, I dont know to play with these '/mailto:([^"]+)">/'
is there some tutorials to lern this about charecters or something..
Thank you...

Oct 22 '05 #3

P: n/a
NurAzije wrote:
a beginner in PHP, I dont know to play with these '/mailto:([^"]+)">/'


It's a "Regular Expression".
Start at http://php.net/manual/en/ref.pcre.php

--
E. Dronkert
Oct 22 '05 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

NurAzije wrote:
$lines = file_get_contents($url);
$done=explode("@",$lines);


I think here lies your problem. explode() expects its second parameter to be
an string, but you are feeding it an array (file() always returns an
array). As a result of the automatic type casting, $done may be empty
everytime yourun the script.

You can either:
- - Use array_map to apply the explode() function to every element from
$lines, and then array_merge the results,
- - or use file_get_contents() instead of file(), that returns a string.

As usual, more information in php.net/manual ...

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.4.2, and PHP
5.0.5-3 generating this signature.
Uptime: 12:47:15 up 15:50, 2 users, load average: 0.38, 0.43, 0.35

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDWhlK3jcQ2mg3Pc8RAsboAJ9jjcxTe0DLSC4UGhctFI SP3ItfBwCfVENu
c+TD9oiDNhjcYxAwYQoo5Qw=
=knA6
-----END PGP SIGNATURE-----
Oct 22 '05 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

NurAzije wrote:

Oh, and, by the way:
for($i=0;$i<count($done)+1;$i++)
Always use foreach() to transverse array: it's quite more secure (doesn't
miss any array keys, even if non-numeric) and compact (i.e. readable).

[...] $a=strrpos($done[$i],":");


Also, if you are not going to use variable expansion (putting a variable
name inside a double-quote-enclosed string), use single quotes instead. It
saves the script a very small little bit of parsing time (that always
helps).

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

"If you haven't got anything nice to say about anybody, come sit next to
me."
- Alice Roosevelt Longworth (1884-1980)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDWhnT3jcQ2mg3Pc8RAoyYAJ9ozRD4EwSuHe9lK3QSUF Ie2+ZIiQCffluZ
/b3Z0xAOSf64CnbG3z8tFK0=
=SdHn
-----END PGP SIGNATURE-----
Oct 22 '05 #6

P: n/a
Iván Sánchez Ortega wrote:
NurAzije wrote:
$lines = file_get_contents($url);
$done=explode("@",$lines);


I think here lies your problem. explode() expects its second parameter to be
an string, but you are feeding it an array (file() always returns an
array). [...]
or use file_get_contents() instead of file(), that returns a string.


He did!

--
E. Dronkert
Oct 22 '05 #7

P: n/a
The expresions way is better, thanks ..
Now I must construct an expression that has an @ at the middle, and ><
at the sides, or spaces or " ", there is a list but the base is the
same ..

Oct 22 '05 #8

P: n/a
I am trying to write the expression like this, but something seems
wrong:
preg_match_all('/(<|:| |").+@.+\....(>| |")/', $data, $match);
as follows :
(<|:| |") -- < or : or space or "
..+ -- any number of any charecters
@ -- is the @ in the email
..+ -- any number of any charecters
\. -- an escape for the dot
.... -- any three characters
(>| |") -- > or space or "

Any help please

Oct 22 '05 #9

P: n/a
NurAzije wrote:
preg_match_all('/(<|:| |").+@.+\....(>| |")/', $data, $match);


Greedy/non-greedy problem, I guess. (Also, you should enclose the part
that you *do* want to match in brackets, or the matches won't show up as
separate entries in $match.) Try '/[a-z0-9_.+-]+@[a-z0-9.-]+/i'.

--
E. Dronkert
Oct 22 '05 #10

P: n/a
I tried that but did not succed yet, I've written a more complexed one:
preg_match_all('/(([A-Za-z0-9_\.-]+)@(?:[a-z0-9]+\-*(?:[a-z0-9A-Z]+)*\.)+[A-Za-z]{2,4})/',
$data, $match);
but even this one can't solve the problem, I need emails..
Thank you.

Oct 22 '05 #11

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ewoud Dronkert wrote:
Iván Sánchez Ortega wrote:
NurAzije wrote:
$lines = file_get_contents($url);
$done=explode("@",$lines);


or use file_get_contents() instead of file(), that returns a string.


He did!


Must read the code slower and more carefully.... :-S

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDWjrH3jcQ2mg3Pc8RAvGRAKCEkOZa679KUgo91lezuR GiWhZs0wCcCLBI
/YKgN9RAs8YoeETbmnBmvTI=
=nHM4
-----END PGP SIGNATURE-----
Oct 22 '05 #12

P: n/a
"NurAzije" <nu******@gmail.com> wrote:

I have written a code and can't figure what is wrong, the code will
read a content of a file and cut all the emails from it,then echo them,
but I have a problem with '"' charecter I think..


You have no idea how hard this problem really is. The regular expression
which completely matches all possible RFC822 e-mail addresses is more than
6,000 characters long.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Oct 23 '05 #13

P: n/a
is it something like this :
************
<?
$data = file_get_contents('http://www.bihstudenti.com/kontakt.php');
$match = array();
preg_match_all('/^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/',
$data, $match);
print_r($match[1]);
?>
**********************
But I still get this result in printout :
Array()

What to do more..
Any ideas

Oct 23 '05 #14

P: n/a
NurAzije wrote:
print_r($match[1]);

But I still get this result in printout :
Array()


It's definitely not the right expression. But first of all try
print_r($match) and go from there. (Not ...[1].)

--
E. Dronkert
Oct 23 '05 #15

P: n/a
thank you I changed it..
I have made a code which will work well for my home work, don't need
more than that:
<?
$data =
file_get_contents('http://www.php.net/manual/en/control-structures.foreach.php');
$match = array();
preg_match_all('/[a-zA-Z0-9._-]+(@| at | AT )([a-zA-Z0-9._]+(\.| dot |
DOT )[a-zA-Z0-9]+)/', $data, $match);
for($i=0;$i<count($match[0]);$i++)
{
echo $match[0][$i]."<br />";
}
?>
Thank you all, I learnd something new, Expressions..

Oct 23 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.