Post by felis there an equivalent to php's str_replace() in RPG || CL ?Look at the %replace() RPG built in function.
nothing in CL?
you could use a combination of the QCLSCAN program and the %sst built
in function in CL,
The pure ILE way of doing something like this is to write a service
program procedure in RPG or C and then use the CALLPRC command in CL to
call that procedure.
It is not as hard as it might look, but nowhere near the facilities of
PHP. CL and even RPG has nothing like PHP arrays.
here is the CL:
/* test20c - sample use of str_replace procedure. */
PGM
dcl &res *char 512
dcl &addr *char 256
dcl &company *char 256
dcl &find *char 80
dcl &rpl *char 80
chgvar &addr '52 empire blvd'
chgvar &find 'blvd'
chgvar &rpl 'boulevard'
callprc prc('str_replace') +
parm(&res &addr &find &rpl)
sndpgmmsg msg('Before:' *bcat &addr *bcat 'After:' +
*bcat &res)
chgvar &company 'IBM IBM IBM'
callprc prc('str_replace') +
parm(&res &company 'IBM' 'Microsoft')
sndpgmmsg msg('Before:' *bcat &company *bcat 'After:' +
*bcat &res)
ENDPGM
here is the rpg procedure:
** ------------------ str_replace ---------------------------
** scan and replace in string
dstr_replace...
d pr opdesc
d extproc('str_replace')
d OutString 2000a options(*VarSize)
d InString 2000a const options(*VarSize)
d InFind 2000a const options(*VarSize)
d InReplace 2000a const options(*VarSize)
** ------------------ str_replace ---------------------------
** scan and replace in string
pstr_replace...
p b export
dstr_replace...
d pi opdesc
d OutString 2000a options(*VarSize)
d InString 2000a const options(*VarSize)
d InFind 2000a const options(*VarSize)
d InReplace 2000a const options(*VarSize)
d str s 2000a varying
d find s 2000a varying
d rpl s 2000a varying
d i10 s 10i 0
d argSx s 10i 0
d fx s 10i 0
d ix s 10i 0
d remSx s 10i 0
/free
// string to scan/replace in.
ceedod( 2: i10: i10: i10: i10: argSx: *omit ) ;
str = %trimr(%subst(InString:1:ArgSx)) ;
// find and replace values.
ceedod( 3: i10: i10: i10: i10: argSx: *omit ) ;
find = %trimr(%subst(InFind:1:ArgSx)) ;
ceedod( 4: i10: i10: i10: i10: argSx: *omit ) ;
rpl = %trimr(%subst(InReplace:1:ArgSx)) ;
// scan and replace
ix = 1 ;
dow 1 = 1 ;
remSx = %len(str) - ix + 1 ;
if %len(find) > remSx ;
leave ;
endif ;
fx = %scan( find: str: ix ) ;
if fx = 0 ;
leave ;
endif ;
str = %replace( rpl: str: fx: %len(find)) ;
ix = fx + %len(rpl) ;
enddo ;
// return the result string.
ceedod( 1: i10: i10: i10: i10: argSx: *omit ) ;
%subst(OutString:1:argSx) = str ;
/end-free
p e