Re: regular expression

From: Date: Mon, 30 Oct 2000 23:51:53 +0000
Subject: Re: regular expression
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-22780@lists.php.net to get a copy of this message
Cody Caughlan writes: > Hello, > > I have the PERL regexp that I am triying to implement in PHP and I am > having a hard time, anybody have any ideas? > > $str =~ s/(\w{$len})/$1-<BR>/g; > > This replaces every $len characters of $str with a hyphen and HTML > newline. > > I tried using various preg_match() calls and I just couldnt get it to > work.. > > Thanks in advance, > /Cody Caughlan First, it usually helps if you show what you've tried so we have some idea of what you're going for--not everybody speaks perl. :) In any case, preg_match() would only tell you whether the pattern matched. For replacements, you'd need preg_replace(): <?php error_reporting(E_ALL); $len = 8; $str = "This is a testable, prettymuchuseless string for this thing."; $str = preg_replace('/(\w{' . $len . '})/', '\1-<BR>', $str); echo htmlspecialchars($str); ?> Note that $len is broken out of the matching pattern here--that's because the sequence {$ has special significance in PHP strings (it's used for resolving ambiguities when embedding complex variables inside double-quoted strings). Hope this helps, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#22780) next »