Jump to content

[SOLVED] need to pull some text from a string using substr


Looktrne

Recommended Posts

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

<?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];

?>

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

$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.

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

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;

Archived

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

×
×
  • Create New...

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.