soadlink Posted September 12, 2006 Share Posted September 12, 2006 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\test@email.com\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! Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 12, 2006 Share Posted September 12, 2006 My method, while logical in my head, doesn't work ;) Quote Link to comment Share on other sites More sharing options...
soadlink Posted September 12, 2006 Author Share Posted September 12, 2006 Thanks for replying anyway 8). I was tinkering around with it before your post edit and couldnt get it to work myself. Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 12, 2006 Share Posted September 12, 2006 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 Quote Link to comment Share on other sites More sharing options...
soadlink Posted September 12, 2006 Author Share Posted September 12, 2006 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! Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 12, 2006 Share Posted September 12, 2006 Guess it works as long as all your search strings are set up the same way :) Quote Link to comment Share on other sites More sharing options...
effigy Posted September 12, 2006 Share Posted September 12, 2006 [code]<pre><?php echo $string = '5345345534883\email\test@email.com\sig\\'; preg_match('/(?<=\\\email\\\)(.+?)(?=\\\sig\\\)/', $string, $matches); array_shift($matches); print_r($matches);?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 13, 2006 Share Posted September 13, 2006 or[code]<?phpfunction emailFilter($item) {return strpos($item, '@');}$string = "5345345534883\\email\\test@email.com\\sig\\";$x = array_filter(explode('\\', $string), 'emailFilter');echo current($x);?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.