Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

ereg_replace

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ereg_replace

Postby movingalong » Wed Jul 16, 2003 12:30 am

I am new to php and finding my way slowly. I'm hoping that someone can assist me please. I am replacing placeholders in the following:

[[place01]]Z[[place02]]Z[[place03]]Z[[place04]]Z[[place05]]Z[[place06]]Z[[place07]]

with actual values ($figure) entered by user on a form

When $figure values are as follows:

place01: grey cloud
place02: brown horse
place03: black sheep
place04: green grass
and place 05, place 06 and place 07 are empty fields

and I use this code:

$result = ereg_replace( "[[$placeholder]]", $figure, $result );
$result = ereg_replace( "Z{2,}", "", $result );

I get this output:

grey cloudZbrown horseZblack sheepZgreen grassZ
------------------------------------------------------------------------------------

I then want to replace each Z with a single Y

When I add a third replace step:

$result = ereg_replace( "[[$placeholder]]", $figure, $result );
$result = ereg_replace( "Z{2,}", "", $result );
$result = ereg_replace( "Z", "Y", $result );

I get this output:

grey cloudYbrown horseYblack sheepYgreen grassYYY

I don't understand why three Y's have appeared and not a single Y.

My question: How can I get the third replace step above to give:

grey cloudYbrown horseYblack sheepYgreen grassY

I know that I can simply use Y instead of Z in the second replace step. However Y is actually a sequence of characters (it's a bit of an explanation) and I need to complete step 2 before moving to step 3.

I would welcome any advice please.
movingalong
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Jul 15, 2003 11:52 pm

Postby Joel » Wed Jul 16, 2003 2:24 am

If you want to replace the place01 with values entered from a form. I think you have gone the complete wrong way in doing this. Try this code.

Code: Select all
//Use $_GET if you pass the variables using get
$y = "Sequence of characters";
$result = $_POST['place01'].$y.$_POST['place02'].$y;


Then add on how many more you want. This will produce an output of just what you want.

You said Y is a sequence of characters. So I stored the sequence in a variable called $y.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby movingalong » Thu Jul 17, 2003 4:38 am

Thank you for your suggestion Joel. I will try it.
movingalong
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Jul 15, 2003 11:52 pm

Postby movingalong » Sun Jul 27, 2003 2:19 pm

:) Joel - my apologies for not giving you some feedback regarding your suggestion sooner. Thank you for your help. It has set me on the right path.
movingalong
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Jul 15, 2003 11:52 pm

Postby Joel » Sun Jul 27, 2003 11:57 pm

Thats OK. :lol:
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.