473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Optimum way

What would be the optimum code for replacing string "xxx" with "yyyyyyy" in
a file? (maybe with str_replace function?)

Thanks,
Boris Savc
Jul 17 '05 #1
10 2376

"Boris Šavc" <bo********@odis-info.com> wrote in message
news:7s***************@news.siol.net...
What would be the optimum code for replacing string "xxx" with "yyyyyyy" in a file? (maybe with str_replace function?)

Thanks,
Boris Savc


but from point where I can find text "START" to point where I can find text
"STOP" only!

Thanks to you all,
Boris Savc

Jul 17 '05 #2
Boris Šavc wrote:
"Boris Šavc" <bo********@odis-info.com> wrote in message
What would be the optimum code for replacing string "xxx" with "yyyyyyy"
in a file? (maybe with str_replace function?)
but from point where I can find text "START" to point where I can find text
"STOP" only!


I might do that using
http://www.php.net/strpos
http://www.php.net/substr
http://www.php.net/str_replace
<?php
// find the first "START" in $contents
$start = strpos($contents, 'START');
if ($start !== false) {
// find first "STOP" *after* the start
$stop = strpos($contents, 'STOP', $start);
if ($stop !== false) {
$partial_contents = substr($contents, $start, $stop-$start);
$partial_contents = str_replace('xxx', 'yyyyyyy', $partial_contents);
$contents = substr($contents, 0, $start)
. $partial_contents
. substr($contents, $stop);
}
}
// done! $contents changed
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3

"Pedro Graca" <he****@hotpop.com> wrote in message
news:br************@ID-203069.news.uni-berlin.de...
Boris Šavc wrote:
"Boris Šavc" <bo********@odis-info.com> wrote in message
What would be the optimum code for replacing string "xxx" with "yyyyyyy" in a file? (maybe with str_replace function?)
but from point where I can find text "START" to point where I can find

text "STOP" only!


I might do that using
http://www.php.net/strpos
http://www.php.net/substr
http://www.php.net/str_replace
<?php
// find the first "START" in $contents
$start = strpos($contents, 'START');
if ($start !== false) {
// find first "STOP" *after* the start
$stop = strpos($contents, 'STOP', $start);
if ($stop !== false) {
$partial_contents = substr($contents, $start, $stop-$start);
$partial_contents = str_replace('xxx', 'yyyyyyy', $partial_contents);
$contents = substr($contents, 0, $start)
. $partial_contents
. substr($contents, $stop);
}
}
// done! $contents changed
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--


Thanks a million times. Great pointers. Have you got maybe some idea how to
find "TEXT 9999" (9999 are random numeric fields) with "TEXT 99 TEXT 99"?

Regards,
Boris Savc
Jul 17 '05 #4
Boris Savc wrote:
Have you got maybe some idea how to
find "TEXT 9999" (9999 are random numeric fields) with "TEXT 99 TEXT 99"?


????

Try regexps:
http://www.php.net/PCRE

When you start using them you might find "The regex coach" [1] a very
good tool.

[1] http://www.weitz.de/regex-coach/
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5
Hi Boris Savc,

Using PCRE function preg_replace the required search and replace can
be implemented by following code:

[SNIP]

$contents = "sdkfnajk nsdnfkafk nsdkf a START jsdfnjkan ndk fnkkfg
xxxkmkxlklxkkxxxx STOP nsdfhvklahdfkh ansbnfj ajdfh xxx fSTOP sdjfh
sdkfj jasdfkl START dsf xxxxxxx h kljsdfh STOP dskfjl; kj;xxx xxxxx";

echo preg_replace("/(START.*xxx.*STOP)/Ue","str_replace('xxx','yyyy','$1')",$contents);

[/SNIP]

This code will replcae all xxx between START & STOP with yyyy.

-- Rahul
"Boris Šavc" <bo********@odis-info.com> wrote in message news:<zP***************@news.siol.net>...
"Boris Šavc" <bo********@odis-info.com> wrote in message
news:7s***************@news.siol.net...
What would be the optimum code for replacing string "xxx" with "yyyyyyy"

in
a file? (maybe with str_replace function?)

Thanks,
Boris Savc


but from point where I can find text "START" to point where I can find text
"STOP" only!

Thanks to you all,
Boris Savc

Jul 17 '05 #6

"Pedro Graca" <he****@hotpop.com> wrote in message
news:br************@ID-203069.news.uni-berlin.de...
Boris Savc wrote:
Have you got maybe some idea how to
find "TEXT 9999" (9999 are random numeric fields) with "TEXT 99 TEXT
99"?
????

Try regexps:
http://www.php.net/PCRE

When you start using them you might find "The regex coach" [1] a very
good tool.

[1] http://www.weitz.de/regex-coach/
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--


I can find TEXT 9999 and change it to some TEXT but how about changing it to
TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
expression. Any ideas?

Thanks,
Boris Savc
Jul 17 '05 #7
Boris Šavc wrote:
I can find TEXT 9999 and change it to some TEXT but how about changing it to
TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
expression. Any ideas?


Regexps are your friends :)

<?php
$x = 'abc TEXT 5496 def TEXT 0901 ghi';
$y = preg_replace('/TEXT (\d{2})(\d{2})/', 'TEXT1 $1 TEXT2 $2', $x);
echo $x, ' -- ', $y, "\n";
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #8

"Pedro Graca" <he****@hotpop.com> wrote in message
news:br************@ID-203069.news.uni-berlin.de...
Boris Šavc wrote:
I can find TEXT 9999 and change it to some TEXT but how about changing it to TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
expression. Any ideas?


Regexps are your friends :)

<?php
$x = 'abc TEXT 5496 def TEXT 0901 ghi';
$y = preg_replace('/TEXT (\d{2})(\d{2})/', 'TEXT1 $1 TEXT2 $2', $x);
echo $x, ' -- ', $y, "\n";
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--


Thanks again Pedro!

You have really all the answers. Your tip for Regex Coach was great also.
I'm using it all the time. Hope I'll learn my regexps :-) But still one more
question: If I want to do the same with characters let's say XTEXTY change
with XTEXT TEXT TEXT Y how to do that? (\w finds it, but how to do a
replacement?)

Regards,
Boris Savc
Jul 17 '05 #9
Boris Savc wrote:
But still one more
question: If I want to do the same with characters let's say XTEXTY change
with XTEXT TEXT TEXT Y how to do that? (\w finds it, but how to do a
replacement?)


This one I'll let you figure out by yourself :-)

Parenthesis in the pattern _grab_ their contents into $1, $2, ...
and you can use these references in the substitution part

preg_replace('/
TEXT # match literal TEXT
(\d\d) # match two digits and grab into $1
(\d\d) # match two digits and grab into $2
/x', ## specify extended syntax for regexp

'TEXT $1 TEXT $2', # replace with TEXT (space)
# whatever was grabbed into $1
# (space) TEXT (space)
# and whatever was grabbed into $2
'TEXT8764');

Happy regexing :-)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #10

"Boris Savc" <bo********@odis-info.com> wrote in message
news:8k*****************@news.siol.net...

"Pedro Graca" <he****@hotpop.com> wrote in message
news:br************@ID-203069.news.uni-berlin.de...
Boris Šavc wrote:
I can find TEXT 9999 and change it to some TEXT but how about changing it to TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
expression. Any ideas?
Regexps are your friends :)

<?php
$x = 'abc TEXT 5496 def TEXT 0901 ghi';
$y = preg_replace('/TEXT (\d{2})(\d{2})/', 'TEXT1 $1 TEXT2 $2', $x);
echo $x, ' -- ', $y, "\n";
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--


Thanks again Pedro!

You have really all the answers. Your tip for Regex Coach was great also.
I'm using it all the time. Hope I'll learn my regexps :-) But still one

more question: If I want to do the same with characters let's say XTEXTY change
with XTEXT TEXT TEXT Y how to do that? (\w finds it, but how to do a
replacement?)

Regards,
Boris Savc


Sorry. Found it. It's the same way like with numbers.

Thanks and have a great day,
Boris
Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
0
by: Used Cisco at Optimum Data | last post by:
9-29-04 Thank you for this opportunity to work with you again, I really appreciate it. Here is a list of our current specials: 3662 128DRAM, 16 Flash, AC $4900.00 3640 32DRAM, 8 Flash, AC...
0
by: Sam | last post by:
I'm putting gtkhtml2.View() inside a gtk.ScrolledWindow, which goes into a gtk.Dialog.vbox. How do I obtain gtkhtml2.View's preferred height, and set the dialog's height accordingly, given a...
3
by: Anurag | last post by:
Hi, (1) Wish to find out what size should I set "util_heap_sz" to for optimum backup performance. Environment: DB2 8.1 FP4 on AIX (Will not be using COMPRESS option as it stores both the...
0
by: dankyy1 | last post by:
hi all i got a problem ....as there are a bill pool.all bills have a cost $ and the last date . from this pool i want to get optimum selection that near to equals to money and the date that...
4
by: dsdevonsomer | last post by:
Hello friends, I have one simple question. I have two tables. 1 ( Table A ) has about 2.5 million rows and second one ( Table B ) has about 1 million. There are common ID fields in both tables. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.