Re: [PHP] Using eregi_replace()
On Fri, Aug 01, 2003 at 10:33:50AM -0400, Anthony Ritter wrote: Using eregi_replace(), is there a way to take out a piece of a sentence - which has spaces - and then return the new sentence? For example, to return the new sentence: hello I must be going from the original sentence: blah blah blah hello I must be going blah blah. I tried: ... ? $text=blah blah blah hello I must be going blah blah; $text= eregi_replace((hello.)(.going),$newtext,$text); you mean $newtext= ereg_replace(.*?(hello.*going).*,\\1,$text); ?? i would suggest preg_replace in favour to ereg_replace: $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); it is more powerfull, faster and widely available. greetings messju echo $newtext; ? ... Thank you. Tony Ritter -- 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
Re: [PHP] Using eregi_replace()
* Thus wrote messju mohr ([EMAIL PROTECTED]): On Fri, Aug 01, 2003 at 10:33:50AM -0400, Anthony Ritter wrote: i would suggest preg_replace in favour to ereg_replace: $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); Doesn't the $1 need to be escaped or in single quotes so php doesn't touch it. $newtext= preg_replace(!.*?(hello.*going).*!,\$1,$text); or $newtext= preg_replace(!.*?(hello.*going).*!,'$1',$text); I cant test it at the moment. Thanks it is more powerfull, faster and widely available. I agree, I like now that PREC is now compiled by default. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using eregi_replace()
And besides ereg_replace would not work becase it's greedy, .* would match going, than going would not be found and the whole match would fail. messju mohr wrote: you mean $newtext= ereg_replace(.*?(hello.*going).*,\\1,$text); ?? i would suggest preg_replace in favour to ereg_replace: $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); it is more powerfull, faster and widely available. greetings messju echo $newtext; ? ... Thank you. Tony Ritter -- 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
Re: [PHP] Using eregi_replace()
Messju Mohr [EMAIL PROTECTED] writes: you mean $newtext= ereg_replace(.*?(hello.*going).*,\\1,$text); ?? .. Thank you but I get: Warning: REG_BADRPT: in c:\apache\htdocs\string.php on line 3 Using: . ? $text=blah blah blah hello I must be going blah blah; $newtext= eregi_replace(.*?(hello.*going).*,\\1,$text); echo $newtext; ? Regards, TR -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using eregi_replace()
On Fri, Aug 01, 2003 at 03:13:53PM +, Curt Zirzow wrote: * Thus wrote messju mohr ([EMAIL PROTECTED]): On Fri, Aug 01, 2003 at 10:33:50AM -0400, Anthony Ritter wrote: i would suggest preg_replace in favour to ereg_replace: $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); Doesn't the $1 need to be escaped or in single quotes so php doesn't touch it. sorry, i wanted to say '$1' and not $1. $newtext= preg_replace(!.*?(hello.*going).*!,\$1,$text); or $newtext= preg_replace(!.*?(hello.*going).*!,'$1',$text); I cant test it at the moment. Thanks it is more powerfull, faster and widely available. I agree, I like now that PREC is now compiled by default. Curt -- I used to think I was indecisive, but now I'm not so sure. -- 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
Re: [PHP] Using eregi_replace()
On Fri, Aug 01, 2003 at 11:25:07AM -0400, Anthony Ritter wrote: Messju Mohr [EMAIL PROTECTED] writes: you mean $newtext= ereg_replace(.*?(hello.*going).*,\\1,$text); ?? .. Thank you but I get: Warning: REG_BADRPT: in c:\apache\htdocs\string.php on line 3 grr, of course. ereg doesn't know .*?. you will have to use .*, but this will match the most right hello not the most left one. as i said: use preg_replace. Using: . ? $text=blah blah blah hello I must be going blah blah; $newtext= eregi_replace(.*?(hello.*going).*,\\1,$text); echo $newtext; ? Regards, TR -- 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
Re: [PHP] Using eregi_replace()
On Fri, Aug 01, 2003 at 05:13:05PM +0200, Marek Kilimajer wrote: And besides ereg_replace would not work becase it's greedy, .* would match going, than going would not be found and the whole match would fail. nope, it would backtrace then. but the first .* ist greedy so it would match the last hello, not the first one if there is more than one hello in the string. messju mohr wrote: you mean $newtext= ereg_replace(.*?(hello.*going).*,\\1,$text); ?? i would suggest preg_replace in favour to ereg_replace: $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); it is more powerfull, faster and widely available. greetings messju echo $newtext; ? ... Thank you. Tony Ritter -- 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using eregi_replace()
However, this works using: preg_replace() . ? $text=blah blah blah hello I must be going blah blah; $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); echo $newtext; ? Thank you all. Is there a way I can be sure of the syntax? !.*?(hello.*going).*!, // the pattern which - I think - reads as follows: !.*? // do not use any character or characters before the grouping of hello (hello.*going) // get the grouping of hello and any character after that word to going .*! // do not use any character or characters after the grouping of going - not sure why the exclamation is at the end if it is a negation symbol. Please advise. Thank you. TR $1, // the grouping $text // the original string -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using eregi_replace()
On Fri, Aug 01, 2003 at 11:36:29AM -0400, Anthony Ritter wrote: However, this works using: preg_replace() . ? $text=blah blah blah hello I must be going blah blah; $newtext= preg_replace(!.*?(hello.*going).*!,$1,$text); echo $newtext; ? Thank you all. Is there a way I can be sure of the syntax? !.*?(hello.*going).*!, // the pattern which - I think - reads as follows: !.*? // do not use any character or characters before the grouping of hello (hello.*going) // get the grouping of hello and any character after that word to going .*! // do not use any character or characters after the grouping of going - not sure why the exclamation is at the end if it is a negation symbol. nearly all that. but the ! are not part of the expression, they are the delimiters that sourround the expression: you could also use different delimiters and write for example: '/.*?(hello.*going).*/' or '[.*?(hello.*going).*]' the regular expression itself is merely .*?(hello.*going).* which matches the whole string and replaces it by the first matching expression sourrounded in by ()s (thats the '$1'). Please advise. Thank you. TR $1, // the grouping $text // the original string -- 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
Re: [PHP] Using eregi_replace()
you could also use different delimiters and write for example: '/.*?(hello.*going).*/' or '[.*?(hello.*going).*]' the regular expression itself is merely .*?(hello.*going).* which matches the whole string and replaces it by the first matching expression sourrounded in by ()s (thats the '$1'). just a minor correction, i don't believe you could use '[.*?(hello.*going).*]' as the delimiters have to be the same character. the reason you use delimiters is so that you can use PREG modifiers after the last delimiter, but still inside the expression. read up in the manual, as the posibilities of PREG are enourmous... -skate- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using eregi_replace()
On Fri, Aug 01, 2003 at 04:51:38PM +0100, skate wrote: you could also use different delimiters and write for example: '/.*?(hello.*going).*/' or '[.*?(hello.*going).*]' the regular expression itself is merely .*?(hello.*going).* which matches the whole string and replaces it by the first matching expression sourrounded in by ()s (thats the '$1'). just a minor correction, i don't believe you could use '[.*?(hello.*going).*]' as the delimiters have to be the same character. http://php.net/manual/en/ref.pcre.php : ... Since PHP 4.0.4, you can also use Perl-style (), {}, [], and matching delimiters. ... the reason you use delimiters is so that you can use PREG modifiers after the last delimiter, but still inside the expression. read up in the manual, as the posibilities of PREG are enourmous... full ACK :) -skate- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] using eregi_replace to replace only substrings -- not the entire string.
On Tue, 14 Aug 2001 12:23, Kurt Lieber wrote: I'm trying to update a number in a text file using eregi_replace. The original line is: $registrationLimit = 100; and I'd like to change it to: $registrationLimit = 300; now, 100 appears multiple times throughout the file, so I have to match on the whole string, not just 100. I used the following code: (this may wrap in your mail client) $contents = eregi_replace(registrationLimit = ([0-9]+);,registrationLimit = . $form_registrationLimit . ;,$contents); which works, but I'd like to find a more elegant way to do this, rather than rebuilding the entire string. I tried: $contents = eregi_replace(registrationLimit = ([0-9]+);,\\1$form_registrationLimit,$contents); but that only produces: $100300 (in other words, it combines the two values and strips out the registrationLimit = part...) After reading the manual, I understand why, but I'm hoping there's some similar such method where I can parenthesize a substring in the string pattern and then replace only that substring with my replacement string. Any thoughts? --kurt That sounds like a task for str_replace() -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA Windows is the best GUI - It always sticks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]