Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Oct 29, 2002, 04:51   #1
Michel V
will code HTML for food
 
Michel V's Avatar
 
Join Date: Sep 2000
Location: Corsica
Posts: 552
preg_replace'ing only the First match - it replaces the Last :(

I've got a problem with preg_replace... I'm using the last parameter to determine how many matches will be replaced, setting it to 1 so that only the first match is replaced.
It would all be fine, if only it replaced the first match. Instead, it replaces the last match.

Is there any way to tell it in which order it should replace matches?
__________________
[blogger: zengun] [blogware contributor: wordpress]
Michel V is offline   Reply With Quote
Old Oct 29, 2002, 05:10   #2
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Re: preg_replace'ing only the First match - it replaces the Last :(

Show us what you're using. What's the string, the regex, the result?
__________________
----Adopt-a-Sig----
Your message here!
randem is offline   Reply With Quote
Old Oct 29, 2002, 05:28   #3
Michel V
will code HTML for food
 
Michel V's Avatar
 
Join Date: Sep 2000
Location: Corsica
Posts: 552
The complete code is there: http://tidakada.com/tagifythis.phps

I make it generate matches and replacement strings dynamically, so the preg_replace call looks like that:
PHP Code:

$text = preg_replace($match, $replace, $text, 1); 

Here's an example of what the regexes look like:
#>([^<]{0,})([^a-zA-Z0-9]{1,})(CSS)([^a-zA-Z0-9]{1})([^>]{0,})<#

And it would be replaced by:
>$1$2<acronym title="Cascading Style Sheets">$3</acronym>$4$5<
__________________
[blogger: zengun] [blogware contributor: wordpress]

Last edited by Michel V; Oct 29, 2002 at 05:37..
Michel V is offline   Reply With Quote
Old Oct 29, 2002, 12:38   #4
Michel V
will code HTML for food
 
Michel V's Avatar
 
Join Date: Sep 2000
Location: Corsica
Posts: 552
*bump*

I simplified the regex into:
#>([^<]{0,}[^a-zA-Z0-9]{0,})(CSS)([^a-zA-Z0-9]{0,}[^>]{0,})<#

To replaced it with:
>$1<acronym title="Cascading Style Sheets">$2</acronym>$3<


But it would still replace only the last match. Anyone have any ideas why it just replaces the last match, and/or how I could fix the regex so that it replaces the first match?
__________________
[blogger: zengun] [blogware contributor: wordpress]
Michel V is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 21:11.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved