sphinx Posted August 20, 2013 Share Posted August 20, 2013 Hello. Is there a way to make preg_match match all content such as: <a href="/cashback.aspx?linkauth=MATCHEVERYTHINGHERE&mpurl=test&usr=test"> The linkauth has a random string on numbers and characters on a webpage that I want to grab based on that format. Thanks. Link to comment https://forums.phpfreaks.com/topic/281380-preg-match-all-characters/ Share on other sites More sharing options...
jazzman1 Posted August 20, 2013 Share Posted August 20, 2013 This will match only letters inside a character class closing with brackets ([ ]), between "=" and "&" symbols. $url = '<a href="/cashback.aspx?linkauth=MATCHEVERYTHINGHERE&mpurl=test&usr=test">'; if(preg_match('~=([A-Za-z]+)&~', $url, $match)) { echo '<pre>'.print_r($match[1], true).'</pre>'; } else { echo 'No match'; } Or.....everything between these two symbols: $url = '<a href="/cashback.aspx?linkauth=MATCHEVERYTHINGHERE&mpurl=test&usr=test">'; if(preg_match_all('~=([A-Za-z]+)&?~', $url, $match)) { echo '<pre>'.print_r($match[1], true).'</pre>'; } else { echo 'No match'; } Link to comment https://forums.phpfreaks.com/topic/281380-preg-match-all-characters/#findComment-1445974 Share on other sites More sharing options...
AbraCadaver Posted August 20, 2013 Share Posted August 20, 2013 I would think with this simple example: $pattern = '#linkauth=([^&]+)'; Link to comment https://forums.phpfreaks.com/topic/281380-preg-match-all-characters/#findComment-1445982 Share on other sites More sharing options...
.josh Posted August 20, 2013 Share Posted August 20, 2013 $pattern = '#(?:\?|&)linkauth=([^&\'"]+)#'; make sure it's linkauth and not someothervarendswithlinkauth. Also cover for if it's the last url param. Link to comment https://forums.phpfreaks.com/topic/281380-preg-match-all-characters/#findComment-1446028 Share on other sites More sharing options...
AbraCadaver Posted August 20, 2013 Share Posted August 20, 2013 Link to comment https://forums.phpfreaks.com/topic/281380-preg-match-all-characters/#findComment-1446029 Share on other sites More sharing options...
sphinx Posted August 20, 2013 Author Share Posted August 20, 2013 This works perfectly. <?php $url = '<a href="/cashback.aspx?linkauth=MATCHEVERYrtyTHINGHERE&mpurl=test&usr=test">'; if(preg_match('~=([A-Za-z]+)&~', $url, $match)) { echo '<pre>'.print_r($match[1], true).'</pre>';} else { echo 'No match'; } ?> However, I need it to grab the content from a specific URL in the exact same format. Link to comment https://forums.phpfreaks.com/topic/281380-preg-match-all-characters/#findComment-1446035 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.