467,858 Members | 1,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,858 developers. It's quick & easy.

splitting merged words but www adresses (regexp)

Is there any way to split all merged words but www and e-mail addresses?

I have regexp

preg_replace("/(\.)([[:alpha:]])/", "\\1 \\2", "www.google.com
any,merged.words my****@domain.com")

it give me incorrect result:
www. google. com any, merged. words mymail@domain. com

i need result
www.google.com any, merged. words my****@domain.com

in my case, all web addresses has www. or http:// in beggining of string
and email of course @ inside string

is it possible to write regexp like this?
Jul 17 '05 #1
  • viewed: 2208
Share:
2 Replies

"Piotr" <pi**@gaztea.pl> wrote in message
news:1e*******************************@40tude.net. ..
Is there any way to split all merged words but www and e-mail addresses?

I have regexp

preg_replace("/(\.)([[:alpha:]])/", "\\1 \\2", "www.google.com
any,merged.words my****@domain.com")

it give me incorrect result:
www. google. com any, merged. words mymail@domain. com

i need result
www.google.com any, merged. words my****@domain.com

in my case, all web addresses has www. or http:// in beggining of string
and email of course @ inside string

is it possible to write regexp like this?


No. You would use a lookbehind assertion in instances like these, but the
assertion has to be fixed length. Since a domain name can be of any number
of characters, you can't do it.

What you can do is first search for domain names and email addresses,
replacing them with some placeholders, fix the merged words, then replace
the placeholders again. Example:

function encode($m) { return "###" . base64_encode($m[0]) . "###"; }
function decode($m) { return base64_decode($m[1]); }

$s = "www.google.com any,merged.words my****@domain.com";
$s = preg_replace_callback('/\bwww\.[\w\.]+/', 'encode', $s);
$s = preg_replace_callback('/\b[\w\.]+@[\w\.]+/', 'encode', $s);
$s = preg_replace('/([,.])(\w)/', '\1 \2', $s);
$s = preg_replace_callback('/###(.*?)###/', 'decode', $s);

echo $s;
Jul 17 '05 #2
Dnia Tue, 28 Sep 2004 23:37:13 -0400, Chung Leong napisa≥(a):

function encode($m) { return "###" . base64_encode($m[0]) . "###"; }
function decode($m) { return base64_decode($m[1]); }

$s = "www.google.com any,merged.words my****@domain.com";
$s = preg_replace_callback('/\bwww\.[\w\.]+/', 'encode', $s);
$s = preg_replace_callback('/\b[\w\.]+@[\w\.]+/', 'encode', $s);
$s = preg_replace('/([,.])(\w)/', '\1 \2', $s);
$s = preg_replace_callback('/###(.*?)###/', 'decode', $s);

echo $s;


Thanks a lot! it is great solution I searched a long time!
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by RickMuller | last post: by
3 posts views Thread by William Ahern | last post: by
3 posts views Thread by gregpinero | last post: by
17 posts views Thread by Qiangning Hong | last post: by
11 posts views Thread by elrondrules | last post: by
2 posts views Thread by shadow_ | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.