For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2004 > Trouble With Ereg_replace









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trouble With Ereg_replace
W. Paulisse

2004-03-30, 10:35 am

I've tried to figure this out myself but I find myself getting really pissed
off 'cause i can't get the right thing ]:

please help:

$text = "hello i am wondering if you are really so mini like";
$text = "i am wondering if you are really so mini like";
$text = "i, the human";

I want to replace the 'i' in these strings. Not EVERY i. just the one that
is not 'surrounded' by any alphanum char.

the i's in "mini" and "like" and "if" should NOT be replaced.
but i's in "i" and "i,"

I can't figure this out. I've made a list of which things the ereg_replace
must comply. but i don't know how to implent it

a) replace if the I is surrounden by spaces
b) replace if the string starts with an I and then a space
b) replace if the string end with an space and then a I

it should also be possible to replace a word. Not just the I

for expample "any"
$text = "any toughs how this should be done?";
$text = "does anyone know the answer";

any toughs? ^-^
Tia
W. Paulisse




Alvaro G Vicario

2004-03-30, 11:32 am

*** W. Paulisse wrote/escribió (Tue, 30 Mar 2004 16:19:19 +0200):
> a) replace if the I is surrounden by spaces
> b) replace if the string starts with an I and then a space
> b) replace if the string end with an space and then a I


I hope this helps:

preg_replace('/([^\w])i([^\w])/', '\1I\2', $txt)
preg_replace('/^i([^\w])/', 'I\1', $txt)
preg_replace('/([^\w])i$/', '\1I', $txt)

There must be a way to do it in a single pass, though.

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Alvaro G Vicario

2004-03-30, 11:32 am

*** Alvaro G Vicario wrote/escribió (Tue, 30 Mar 2004 17:10:34 +0200):
> There must be a way to do it in a single pass, though.


preg_replace('/^()i([\W])|([\W])i([\W])|([\W])i()$/', '\1\3\5I\2\4\6',
$txt)

Please test carefully before using (I haven't);

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com