JustinK101 Posted April 27, 2009 Share Posted April 27, 2009 I am trying to do a preg_match_all and return any array of the items found. Here is the pattern I am looking for: id="{wildcard}@domain.com" Here is the code I have: $results = array(); preg_match_all('/id=\"*\@domain.com\"/', $html, $results); print_r($result); The output is: Array ( [0] => Array ( ) ) Does that mean it found a match, but how come the text is empty? Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/ Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 <?php $html = "&id=test@test.com some extra stuff \n and id=teststst@test.com and eve some more \n stuff"; $results = array(); preg_match_all('~id=(.*?) ~', $html, $results); echo "<pre>"; print_r($results); die(); ?> Since you did not provide us with an example of what you want to parse, this will have to suffice, it requires that there be a space after the @domain.com to fetch the data. Output of the above: Array ( [0] => Array ( [0] => id=test@test.com [1] => id=teststst@test.com ) [1] => Array ( [0] => test@test.com [1] => teststst@test.com ) ) Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820006 Share on other sites More sharing options...
JustinK101 Posted April 27, 2009 Author Share Posted April 27, 2009 So is there a way to not require a space? Basically here is a context example of code: <td class="cell truncate" truncate="80" id="justin@domain.com">justin@domain.com</td> I am looking for id="{wildcard}@domain.com" should return: id="justin@domain.com" right? Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820009 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 <?php $html = '&id="test@test.com" some extra stuff \n and id="teststst@test.com" and eve some more \n stuff'; $results = array(); preg_match_all('~id="(.*?)"~', $html, $results); echo "<pre>"; print_r($results); die(); ?> Output: Array ( [0] => Array ( [0] => id="test@test.com" [1] => id="teststst@test.com" ) [1] => Array ( [0] => test@test.com [1] => teststst@test.com ) ) Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820011 Share on other sites More sharing options...
JustinK101 Posted April 27, 2009 Author Share Posted April 27, 2009 Premiso: Close, but I don't think the regular expression is looking for @domain.com. Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820013 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Premiso: Close, but I don't think the regular expression is looking for @domain.com. Misundestanding. I thought, when you said @domain.com it was a reference. <?php $html = '&id="test@domain.com" some extra stuff \n test89@domain.com and id="teststst@domain.com" and id="test2@domain.com" and eve some more \n and id="test3@domain.com" stuff'; $results = array(); preg_match_all('~id="(.*?)@domain.com"~s', $html, $results); echo "<pre>"; print_r($results); die(); ?> Output: Array ( [0] => Array ( [0] => id="test@domain.com" [1] => id="teststst@domain.com" [2] => id="test2@domain.com" [3] => id="test3@domain.com" ) [1] => Array ( [0] => test [1] => teststst [2] => test2 [3] => test3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820019 Share on other sites More sharing options...
nrg_alpha Posted April 27, 2009 Share Posted April 27, 2009 $html = '&id="test@domain-x.com" some extra stuff \n and id="teststst@domain.com" and eve some more \n stuff id="whatever"... &id="blah@domain.com"'; preg_match_all('#id="([^"]+@domain.com)"#', $html, $matches); echo "<pre>".print_r($matches[1], true); Output: Array ( [0] => teststst@domain.com [1] => blah@domain.com ) This assumes of course, that you absolutely want ...@domain.com (change this to match the actual domain in question of course). In order to ensure that I capture the domain and not some stuff in some id=".." that is not related, I use a negated character class..[^"]+ So the idea is to sacrifice speed for more accuracy. Quote Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820021 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.