Jump to content


Photo

Parsing information from strings


  • Please log in to reply
7 replies to this topic

#1 soadlink

soadlink
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 12 September 2006 - 07:52 PM

Hello,

Before I ask this quick question, I'll just mention that i did search google and the forums, but did not find exactly what i was looking for, so here goes:

I am looking for a method to parse an email address out of a string of data. Since email addresses can be various legnths, I dont think using something like substr() will work. Is there a method to pull out a string of data using 'before' and 'after' strings?

For example a string can look like this: 5345345534883\email\test@email.com\sig\

So I'm looking for something that could use '\email\' as a before, and \sig\ as an after, and pull out whatever is in the middle of that. In this case, it would be the email.

Im sure there is a particular function to do that, I just dont know what it is  ???

Thanks for the help!
Enjoy the silence

#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 12 September 2006 - 08:12 PM

My method, while logical in my head, doesn't work ;)

#3 soadlink

soadlink
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 12 September 2006 - 08:24 PM

Thanks for replying anyway 8). I was tinkering around with it before your post edit and couldnt get it to work myself.
Enjoy the silence

#4 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 12 September 2006 - 08:27 PM

Well, the explode works except for the fact that what you need to break it up by us an escape character, \ .
Then, array_search apparently only returns a key value if the search string (@) is the entire entry, not just present in it. 

So hopefully someone else has something

#5 soadlink

soadlink
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 12 September 2006 - 08:33 PM

Wintergreen,

Thank you very much. I actually figured it out with explode. For every \, I actually needed to specify it as 2 backslashes \\.

My code ended up being:

$array = explode("\\", $third);
echo $array[10];

$third being the string I was exploding. "\\" was to explode it at every \ in that string.
And the 10 is because the 10th instance of a "\" in my string is where the email is.

Thanks again for the helping!
Enjoy the silence

#6 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 12 September 2006 - 08:35 PM

Guess it works as long as all your search strings are set up the same way :)

#7 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 12 September 2006 - 08:47 PM

<pre>
<?php

	echo $string = '5345345534883\email\test@email.com\sig\\';
	preg_match('/(?<=\\\email\\\)(.+?)(?=\\\sig\\\)/', $string, $matches);
	array_shift($matches);
	print_r($matches);

?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 September 2006 - 12:47 AM

or
<?php
function emailFilter($item) {return strpos($item, '@');}

$string = "5345345534883\\email\\test@email.com\\sig\\";
$x = array_filter(explode('\\', $string), 'emailFilter');
echo current($x);
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users