Re: preg_replace problem
| From: | Ignacio Vazquez-Abrams | Date: | Thu, 18 Jan 2001 17:55:06 +0000 |
| Subject: | Re: preg_replace problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-35401@lists.php.net to get a copy of this message | ||
On Thu, 18 Jan 2001, The Captain wrote:
> I have a regular expression that's not giving me the results i really need.
>
> I have a bunch of strings coming at me that need to be truncated if they're
> over 15 characters... but not to truncate until after it reaches the first
> word.
>
> This regular expression just doesn't seem to want to work 100%:
>
> $myString = preg_replace("/.{15}\w)?\s.*/","\\1",$myString);
>
>
> This will turn:
>
> 'PROJECT - Marketing Pitch'
>
> into
>
> 'PROJECT - Marketing\1'
>
>
> However... when i change it to: /(.{15}\w)+.*/
> everything seems to work fine.
> I can't really use this regex though, because it will keep matching that 15
> character word until it reaches the end. I need it to stop matching after
> it finds the first match (if it needs to match at all).
>
> In perl:
> + <- means match 1 or many times
> * <- means match 0 or many times
> ? <- means match 0 or 1 time
>
> Could someone help me see my error... i'm driving myself bananas trying to
> find it. Perhaps pcre in PHP doesn't support the ? in the regular
> expressions.
>
> Best Regards,
> Rob
>
Try "/^(.{15}\w)+.*/".
--
Ignacio Vazquez-Abrams <ignacio@openservices.net>