Re: please help with regular expression in preg_replace

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Rene,

This looks suspiciously like regex's "greedy" behaviour - it will gobble up everything that matches until you tell it otherwise.

For example, your regex is matching "any character that isn't a dot, followed by a dot."

In host.domain.com, both "host." and "domain." match this regex - and because your regex is "greedy" it's grabbing both, leaving you with "com".

Try adding the "ungreedy" modifier to your regex, like so: $domain = preg_replace( '/[^.]*\./U' , '', $host); (note the additional U in your regex.)

HTH,
Andy

On 29 October2009, at 20:33, Red wrote:


hello, im not a php developer, i just need to rewrite one php file but having problem with understanding syntax of regexp in php.

i need to get domain name from fqdn (for example from $_SERVER ['HTTP_HOST'] )

in sed its working well with "s/[^.]*\.//" , but preg_replace behaves weird.


http_host is for example hostname.domain.com

<?php
$host = $_SERVER['HTTP_HOST'];
exec( "echo $host | sed s/[^.]*\.//", $domain ) ;
echo "$domain[0]"
?>

return "domain.com", but

<?php
$host = $_SERVER['HTTP_HOST'];
$domain = preg_replace( '/[^.]*\./' , '', $host) ;
echo "$domain";
?>

return only "com"

i think when this php page get many hits, its not so wise to call sed everytime, i would like to ask someone for help how to write preg_replace pattern.

thanx

Rene


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux