Jump to content

Parsing information from strings


soadlink

Recommended Posts

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: [i][b]5345345534883\email\[email protected]\sig\[/b][/i]

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!
Link to comment
https://forums.phpfreaks.com/topic/20545-parsing-information-from-strings/
Share on other sites

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
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:

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

$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!
or
[code]<?php
function emailFilter($item) {return strpos($item, '@');}

$string = "5345345534883\\email\\[email protected]\\sig\\";
$x = array_filter(explode('\\', $string), 'emailFilter');
echo current($x);
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.