Looktrne Posted June 29, 2008 Share Posted June 29, 2008 I need to locate some text in a string and pull out what is in the middle I am getting this a bit but need a little more help for example the string is... <td colspan="5" align="right" class="trs" height="20"> <font class="w"> Copyright (c) 2005 Rockabillydate.com<a href="http://www.rockabillydate.com" class=w> Back to homepage</a> <a href="mailto:[email protected]" class=w>Contact Us</a> </font> </td> I need to pull the mail address [email protected] so I need to find mailto: and then I want it to find the ending quote so it knows when the mail address is finished... so far I have $mem = strpos($code, "mailto:"); but then how can I make a substr that gets what is beyond the mailto: ending with the before quote thanks for any methods on this Paul Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/ Share on other sites More sharing options...
papaface Posted June 29, 2008 Share Posted June 29, 2008 <?php function extract_emails_from($string){ preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches); return $matches[0]; } $text = '<td colspan="5" align="right" class="trs" height="20"> <font class="w"> Copyright (c) 2005 Rockabillydate.com<a href="http://www.rockabillydate.com" class=w> Back to homepage</a> <a href="mailto:[email protected]" class=w>Contact Us</a> </font> </td>'; $emails = extract_emails_from($text); echo $emails[0]; ?> Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577165 Share on other sites More sharing options...
Looktrne Posted June 29, 2008 Author Share Posted June 29, 2008 thank you for that function I will put it to use... but I still need to extract the way I mentioned after mailto: and ending before the quote.. because I do not want it to pull any email from the string only the one in between these 2 if the page has 10 emails I need it to pull only the one between mailto: and " thanks for your help your email function will be usfull also Paul Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577169 Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 $code = file_get_contents("url.com"); // Change this URL while(!strpos($code, "mailto:") === false) { $mem = (strpos($code, "mailto:")+strlen("mailto:")); $email = substr($code, $mem, strpos($code, "\", $mem)); $code = substr($code, strpos($code, ($email+strlen($email)))); } Here's a version that works similar to the one I provided you with the other day. I haven't been able to test it but if it doesn't work let me know and I shall. Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577170 Share on other sites More sharing options...
Looktrne Posted June 29, 2008 Author Share Posted June 29, 2008 I will test it but was wondering what is the \ how does it locate the end of the email?? I just want to understand it a bit better and is the while needed if I am only looking for one instance of it? thanks I will test it also Paul Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577182 Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 I shall test it now as I've just spotted an error in it. If you're only going for the one instance then no you wont need the while loop. Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577184 Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Here you go mate, I've just tested this and it works. It simply finds the first occurence of mailto: and retrieves everything between that and the next speech mark. $code = "This is a sample lot of text. <a href=\"mailto:[email protected]\">Email me</a> if you want to."; $mem = (strpos($code, "mailto:")+strlen("mailto:")); $email = trim(substr($code, $mem, strpos($code, "\"", $mem)-$mem)); echo $email; Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577188 Share on other sites More sharing options...
Looktrne Posted June 29, 2008 Author Share Posted June 29, 2008 thanks broken I was confused by the first post because it was missing the /" just had the / thanks again for all your time I will get this down very soon Paul Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577226 Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 No worries! When a thread is solved though could you click the 'Topic Solved' link at the bottom please? It just makes it easier when I'm searching for threads that need help if ones that are currently solved are labeled so. Link to comment https://forums.phpfreaks.com/topic/112424-solved-need-to-pull-some-text-from-a-string-using-substr/#findComment-577230 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.