Jump to content

Archived

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

soadlink

Parsing information from strings

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\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!

Share this post


Link to post
Share on other sites
Thanks for replying anyway 8). I was tinkering around with it before your post edit and couldnt get it to work myself.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
or
[code]<?php
function emailFilter($item) {return strpos($item, '@');}

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

Share this post


Link to post
Share on other sites

×

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.