Jump to content

Editing a form after submitting


malikye

Recommended Posts

Hello,

 

I am very new to PHP. I am working on a project (just an exercise) and I am stump on one of the exercise:

 

Create a self-submitting form page that accepts the user’s first and last name in 2 text

boxes.

- On submission, your script should replace all instances of the letter “s” with the

number “5”, determines the location of the first “e” (if one exists), and evaluates

whether the last 3 characters of the last name are “son”.

- The modified text should be displayed on the page after submission with the first

“e” underlined and the last 3 characters in italics ONLY if they are “son”.

 

I am able to figure out the first part with replacing "s" with "5" (I'm very proud of myself for that) but the other parts is what i am stuck on. to find the first "e", i assume I would use a If Then statement and a Left Function, but from there, I don't know how I get it to "echo" an underline "e". same goes for the last part of the exercise. I assume i would use a Right Function.

 

Any help would be greatly appreciated.

 

Thank you,

 

Malikye

Link to comment
Share on other sites

This sounds like homework, so I'm not posting the answer. However, I can tell you the approach and you can figure it out yourself.

 

To find the first e you can either use some simple regular expressions, or the strpos function.

 

To tell if "son" is the last 3 letters, you could, again, use regular expressions or the substr function.

Link to comment
Share on other sites

thanks scootstah,

 

this isn't really homework - Im 27 years old and not in school anymore. i won't be graded on this project. but i am trying to learn PHP. i have tried the strpos function - i learned it from w3schools.com. according to w3schools:

 

"The strpos() function returns the position of the first occurrence of a string inside another string."

 

- and the example they have is basically returning the position that the string is in. so that example returned "6", but what i am trying to figure out is, how do i make changes to the output because i want to underline the first letter "e" from the form.

 

thanks for your help. i understand you don't want to help because your worried if someone might be cheating on a homework assignment.

 

Link to comment
Share on other sites

scootstah,

 

thank you so much for pointing me to preg_replace. that worked perfectly. here's my code:

 

<?php
echo preg_replace('/e/', '<u>e</u>', $fname, 1);
?>

 

i assume that should work for underlining "son" also. i will give that a try.

 

update:

here is my code for changing "son" if it's the last 3 letters in the name:

 

<?php
echo preg_replace('/son/', '<i>son</i>', $fname, 3);
?>

 

thank you for your help!

 

 

Link to comment
Share on other sites

after playing around with the code, i came up with this solution:

 

<?php
echo preg_replace('/son/', '<i>son</i>',(preg_replace('/e/', '<u>e</u>',(str_replace("s","5",$fname)), 1)), 3); 
?>

 

I'm sure there is a "cleaner" way to do that, but this works just fine.

 

Thank you, scootstah.

Link to comment
Share on other sites

Actually, your pattern for matching son wouldn't work. You'd have to change it a little, like this:

<?php
echo preg_replace('/son$/', '<i>son</i>', $fname);
?>

 

By using the $, we tell it to only look at the end of the string.

 

And yes, you can combine them like so:

$str = 'underline the first e and italics son';

$str preg_replace(
array('/e/', '/son$/'),
array('<u>e</u>', '<i>son</i>'),
$str, 1);

Link to comment
Share on other sites

By using the $, we tell it to only look at the end of the string.

 

Thanks! i tested my code and your code, and i will add the $ sign. i thought the ",3" i put at the end of the code told it to look at the last 3 characters.

 

No, that "3" is the limit on how many times preg_replace will replace something. In the first pattern, to underline the first e, if we didn't have the "1" there it would replace ALL e's. It's not needed for the second pattern, but it's still needed if you combine them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.