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? 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 = "&[email protected] some extra stuff \n and [email protected] 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] => [email protected] [1] => [email protected] ) [1] => Array ( [0] => [email protected] [1] => [email protected] ) ) 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="[email protected]">[email protected]</td> I am looking for id="{wildcard}@domain.com" should return: id="[email protected]" right? 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="[email protected]" some extra stuff \n and id="[email protected]" 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="[email protected]" [1] => id="[email protected]" ) [1] => Array ( [0] => [email protected] [1] => [email protected] ) ) 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. 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="[email protected]" some extra stuff \n [email protected] and id="[email protected]" and id="[email protected]" and eve some more \n and id="[email protected]" stuff'; $results = array(); preg_match_all('~id="(.*?)@domain.com"~s', $html, $results); echo "<pre>"; print_r($results); die(); ?> Output: Array ( [0] => Array ( [0] => id="[email protected]" [1] => id="[email protected]" [2] => id="[email protected]" [3] => id="[email protected]" ) [1] => Array ( [0] => test [1] => teststst [2] => test2 [3] => test3 ) ) 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="[email protected]" some extra stuff \n and id="[email protected]" and eve some more \n stuff id="whatever"... &id="[email protected]"'; preg_match_all('#id="([^"][email protected])"#', $html, $matches); echo "<pre>".print_r($matches[1], true); Output: Array ( [0] => [email protected] [1] => [email protected] ) This assumes of course, that you absolutely want [email protected] (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. Link to comment https://forums.phpfreaks.com/topic/155776-preg_match_all-questions/#findComment-820021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.