Barbara Picci wrote:
> I've a script that must strip a string when it find the first word
> containing at least 4 characters; it must print the content of the
> string before that word, that word, a separator and the rest of the
> string.
>
> I've tried with ereg whit this script ([EMAIL PROTECTED] is the separator):
>
> ereg( '^([^ ]{4,})(.*)$', $testo, $matches);
>
> $contents = "[EMAIL PROTECTED]";
>
> but it is not the right script because if the strin($testo) is "the
> thing is" the script doesn't find nothing and doesn't print. For this
> example the right content of $contents must be:
>
> $contents = "the [EMAIL PROTECTED]";
>
> instead of nothing.

I'm more comfortable with the PCRE functions:

$string = "the thing is";

if (preg_match('/^(.*?\S{4,})(\s*)(.*)$/', $string, $matches)) {
  $contents = $matches[1].'[EMAIL PROTECTED]'.$matches[3]."\n";
  print_r($contents);
}

prints

the [EMAIL PROTECTED]

Your regex is anchoring its search for the first 4 character "word" to the beginning
of the string, which is probably not what you want.

I'm assuming from your description that you want whitespace that appears after the
first 4 character word to be replaced by the delimiter.  Also you didn't specify
what should happen if the four character word happens to appear at the end of the
string.  Also keep in mind that there are other word boundaries besides simply
whitespace, so you might want to use the "\w", "\W" metacharaters instead of "\s"
and "\S".  See "perldoc perlre" for more details...

HTH

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

Reply via email to