Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preg_replace expression

Status
Not open for further replies.

JimFL

Programmer
Jun 17, 2005
131
GB
Hi,

Can anybody help me with the following expression. I am trying to replace an email address in body text with a link.

<?php
$str = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">\\1@\\2.\\3</a> ',$str);
?>


This seems to work fine when replacing an email address on its own but when text follows, it includes the text in the link. If I place a break after the email address again it works.

Please help.

JimFl
 
Suggestion:

Your last sub-expression (.*) captures any char. You should just limit that to wrod characters, hence (\w*) will be better.
 
Thanks alot DR,
That has solved my issue cheers
 
Hi again,
hopefully you can help me out again. The expression did work fine until it came up across an email address like this.

j.h@test.com

The link will come out as h@test.com

It has problems with the "." in the name part of the email. Do you have any other solutions for this??
 
The address shown above run through the pattern should convert correctly. The only way it wouldn't is if you had replaced the first subexpression with (\w*). In that case only PERL word characters (a-zA-Z_) are used.
The first part with the dot character matches all chars except newlines (unless the 's' pattern modifier is appended to the pattern).
You could additionally assert that there is a word boundary:
Code:
$pattern = '/\b(.*)@(.*)\.(\w*)/';
AAlso, the '@' sign does not need to be escaped as it has no meta meaning within PCRE.
 
Ok thanks , I'll give that a try then. To be honest I must get to grips with regular expressions.
 
Don't feel bad - most people never get to grips with regex.
 
I dont seem to be having much luck today.

I have tried using your code above in my function but it doesnt seem to recognize this email address.

j.h@test.com

function change_string($str)
{
//function to replace emails and with links in the bodytext
$pattern = '/\b(.*)@(.*)\.(\w*)/';
$str = preg_replace($pattern,'<a href="mailto:\\1@\\2.\\3">\\1@\\2.\\3</a> ',$str);
$str = preg_replace("/\s( href=\" target=\"_new\">\\1\\2</a>",$str);
return $str;
}


Is there anything glaringly obvious to you? This function is called to replace both email strings and Is has nothing to do with that does it?

Thanks again
 
JimFL,

I tried the expression and it works as long as there is no text before the e-mail address. To make it work better I'd change the first subexpression to be any non-space char:
Code:
$pattern = '/(\S*)@(.*)\.(\w*)/';

 
Hi, Ive found out that the code works fine with standard html textarea but I am using a HTMLAREA formatting object that inserts extra HTML code.. so it is falling over once the HTMLArea inserts extra code. Sorry for not realising this earlier. If I look at the code that is generated by this element it looks something like this back from the database I am storing it in.

<!-- Generated by XStandard version 1.6.0.0 on 2005-08-09T17:40:16 --><p>Test</p>

For just ..
Test

So when I put an email address the following code is generated and put into the database. This explains why the whole of the bodytext is being appearing as a link.

<a href="mailto:<!-- Generated by XStandard version 1.6.0.0 on 2005-08-09T17:43:38 --><p>j.h@test.com"><!-- Generated by XStandard version 1.6.0.0 on 2005-08-09T17:43:38 --><p>j.h@test.com</a> </p>

And the problem is that it thinks the XStandard code if part of the Email address..

Can you help at all with this?
 
I think I could solve this problem if I removed this tag again by a Preg_replace string.

<!-- Generated by XStandard version 1.6.0.0 on 2005-08-09T17:58:55 -->


As the tag is dynamic it would have to try and remove everything with a <!-- Comment -->.

Could this be achieved using the preg_replace tag?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top