Reading the contents of a string from a file

Checking for a character from within a word from a file

Page 1 of 1

8 Replies - 1119 Views - Last Post: 24 May 2009 - 06:43 AM Rate Topic: -----

#1 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Reading the contents of a string from a file

Posted 24 May 2009 - 03:55 AM

I've been wondering how I could read a file comprised of un-condensed data, to print a list of all email addresses from within. I've managed to gather a word count, but I'm not too sure what function I would use to read the words and furthermore, to check for a specific symbol from within each word (e.g. to check for "@" within each word; to print all words composed within the file).

Any help is greatly appreciated! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Reading the contents of a string from a file

#2 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 04:26 AM

Is there only emails in the file? If so, how are they composed? Each on a single line? Comma separated? Something?

If there are more than emails in the file you have to look at regular expressions! :)


View Postlivelonger87, on 24 May, 2009 - 12:55 PM, said:

I've been wondering how I could read a file comprised of un-condensed data, to print a list of all email addresses from within. I've managed to gather a word count, but I'm not too sure what function I would use to read the words and furthermore, to check for a specific symbol from within each word (e.g. to check for "@" within each word; to print all words composed within the file).

Any help is greatly appreciated! :)

Was This Post Helpful? 0
  • +
  • -

#3 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 05:50 AM

View PostWimpy, on 24 May, 2009 - 03:26 AM, said:

Is there only emails in the file? If so, how are they composed? Each on a single line? Comma separated? Something?

If there are more than emails in the file you have to look at regular expressions! :)


View Postlivelonger87, on 24 May, 2009 - 12:55 PM, said:

I've been wondering how I could read a file comprised of un-condensed data, to print a list of all email addresses from within. I've managed to gather a word count, but I'm not too sure what function I would use to read the words and furthermore, to check for a specific symbol from within each word (e.g. to check for "@" within each word; to print all words composed within the file).

Any help is greatly appreciated! :)

I've managed to find the total number of emails by searching for any word comprised with the "@" symbol; however, I don't know how to display a list of all words formed with the "@" symbol - I'm not too sure how I would do this with regular expressions, or if there are alternate string functions that could complete this.

Thanks for the help anyway! It had led to me noticing regular expressions! :D
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 06:12 AM

You could use the preg_match_all() for that, here's an example:
<?php
$content = "Niklas [email protected] awofmpa awofapwfon akwfpaowfpa  a fawfg [email protected] , [email protected]";
preg_match_all('/[a-z0-9._-][email protected][a-z0-9.-]+\.[a-z]{2,6}/i', $content, $match, PREG_PATTERN_ORDER);
foreach($match[0] as $email)
{
	echo $email."<br />";
}
?>

The regular expression to match an email might not be the best, I actually don't know, so you should perhaps google that up! But it works in my example!

Hope it helps! :)
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 06:21 AM

Since you're dealing with a list, you'll probably want to put them in an array, so this might be useful:

<?php
$text = "some text [email protected] more randome words [email protected] Lor ipsum [email protected] [email protected] ddfd [email protected] text</b> here more stuff after...";
$x = explode(" ",$text);
foreach($x as $val) {
	if(strpos($val,"@") != '') {
	$y[] =  $val;
	}
}
foreach($y as $val) {
echo $val."<br>";
}
?>

Was This Post Helpful? 0
  • +
  • -

#6 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 06:27 AM

Only checking if the "@" exist in a word isn't accurate enough, is it? I mean, you can use the @-sign to more than just emails! And also, why do you do this(?):
if(strpos($val,"@") != '')

strpos return a boolean false if the needle was not found! Is "" equal to false? I'm just curious since I've never worked with it in that way!

View PostCTphpnwb, on 24 May, 2009 - 03:21 PM, said:

Since you're dealing with a list, you'll probably want to put them in an array, so this might be useful:

<?php
$text = "some text [email protected] more randome words [email protected] Lor ipsum [email protected] [email protected] ddfd [email protected] text</b> here more stuff after...";
$x = explode(" ",$text);
foreach($x as $val) {
	if(strpos($val,"@") != '') {
	$y[] =  $val;
	}
}
foreach($y as $val) {
echo $val."<br>";
}
?>

This post has been edited by Wimpy: 24 May 2009 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#7 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 06:34 AM

Hey! - wow, thanks for all the help guys! I greatly appreciate it all! - I've managed to extract all emails from the file (There was 12704! :o ), and had even categorized them! Wow, so many emails! Thanks for the script! I greatly appreciate all the help! :D
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 06:41 AM

View PostWimpy, on 24 May, 2009 - 09:27 AM, said:

Only checking if the "@" exist in a word isn't accurate enough, is it? I mean, you can use the @-sign to more than just emails! And also, why do you do this(?):
if(strpos($val,"@") != '')

strpos return a boolean false if the needle was not found! Is "" equal to false? I'm just curious since I've never worked with it in that way!


Yes, I just assumed that in this file an @ means that it is an address.

Apparently a null string does not equal false, since this does not work:

if(!strpos($val,"@"))

This post has been edited by CTphpnwb: 24 May 2009 - 06:41 AM

Was This Post Helpful? 1
  • +
  • -

#9 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Reading the contents of a string from a file

Posted 24 May 2009 - 06:43 AM

Ok, thanks for the input! :)

View PostCTphpnwb, on 24 May, 2009 - 03:41 PM, said:

Apparently a null string does not equal false, since this does not work:
if(!strpos($val,"@"))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1