please help with regular expression in preg_replace

please help with regular expression in preg_replace

am 29.10.2009 21:33:39 von Red

------=_NextPart_000_0115_01CA58DF.7AF04B40
Content-Type: text/plain;
charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable


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

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

return "domain.com", but

$host =3D $_SERVER['HTTP_HOST'];
$domain =3D 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=20

Rene

------=_NextPart_000_0115_01CA58DF.7AF04B40--

Re: please help with regular expression in preg_replace

am 29.10.2009 22:57:30 von andy-lists

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
>
> > $host = $_SERVER['HTTP_HOST'];
> exec( "echo $host | sed s/[^.]*\.//", $domain ) ;
> echo "$domain[0]"
> ?>
>
> return "domain.com", but
>
> > $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

Re: please help with regular expression in preg_replace

am 30.10.2009 00:33:51 von List Manager

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
>
> > $host = $_SERVER['HTTP_HOST'];
> exec( "echo $host | sed s/[^.]*\.//", $domain ) ;
> echo "$domain[0]"
> ?>
>
> return "domain.com", but
>
> > $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
>

I would add one thing and change another.

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

Adding an additional '^' to the start tells it to start at the beginning.
And changing '*' to a '+'

Jim

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

Re: please help with regular expression in preg_replace

am 30.10.2009 08:58:52 von Red

Many thanx to Jim and andy for their replies, with Jim`s changes its now
working like in sed. Excuse me for my stupid question, but im old unix/linux
system engineer coding with C , perl and bash, php gave me unpredictable
results in this task. ;-)
so, in final, may be this help some other roundcube users who need one
universal site for many domain access.
this is test page:

$host = "host.1stdom.com" ;
//$host = "host.2nddom.1stdom.com" ;
$pattern1 = '/[^.]*\./U' ;
$pattern2 = '/^[^.]+\./' ;

exec( "echo $host | sed s/[^.]*\.//", $dom_sed ) ;
$p1_1 = preg_replace( '/[^.]*\./U' , '', $host ) ;
$p1_2 = preg_replace( $pattern1 , '', $host ) ;
$p2_1 = preg_replace( '/^[^.]+\./' , '', $host ) ;
$p2_2 = preg_replace( $pattern2 , '', $host ) ;

echo "$host
" ;
echo "dom_sed = $dom_sed[0]
" ;
echo "p1_1 = $p1_1
" ;
echo "p1_2 = $p1_2
" ;
echo "p2_1 = $p2_1
" ;
echo "p2_2 = $p2_2
" ;

?>
with result:
host.1stdom.com
dom_sed = 1stdom.com
p1_1 = com
p1_2 = com
p2_1 = 1stdom.com
p2_2 = 1stdom.com


and this is line which i need in roundcube setup:

$rcmail_config['username_domain'] = preg_replace( '/^[^.]+\./' , '',
$_SERVER['HTTP_HOST'] ) ;

many thanks again

Rene

----- Original Message -----
From: "Jim Lucas"
To: "Red"
Cc:
Sent: Friday, October 30, 2009 12:33 AM
Subject: Re: [PHP] please help with regular expression in preg_replace


> 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
>>
>> >> $host = $_SERVER['HTTP_HOST'];
>> exec( "echo $host | sed s/[^.]*\.//", $domain ) ;
>> echo "$domain[0]"
>> ?>
>>
>> return "domain.com", but
>>
>> >> $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
>>
>
> I would add one thing and change another.
>
> > $host = $_SERVER['HTTP_HOST'];
> $domain = preg_replace( '/^[^.]+\./' , '', $host) ;
> echo $domain;
> ?>
>
> Adding an additional '^' to the start tells it to start at the beginning.
> And changing '*' to a '+'
>
> Jim
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


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