Editing a form after submitting

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:

[b]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”.[/b]

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

As this is an exercise, I’ll give you the functions that I would use to solve them - so that you can learn how to do it yourself. If you want a solution, reply back and I’ll write it up for you.

To get the first occurrence of a character (or string), you can use the strpos function:

http://php.net/manual/en/function.strpos.php

After testing for the e (if it exists), you could get parts of the string to use with the substr function.

To help you out, I’ve written down the rough steps that I would undertake to solve this problem.

Get the position of e (strpos).
If e is found, get all of the text before that position (substr).
Output all that text.
Output the HTML underline tag

<u>

Output e
Output the HTML underline tag (closing it)

</u>

Test if the last part of the string is “son”
If it is, get the part before the last three letters (substr)
Get the son part (substr)
Output the first part
Output son with italics:
[php]echo ‘’, $son_part, ‘’;[/php]

If son isn’t found, echo everything after the position of e (substr)

As stated above, if you want a solution (or more help), then reply back and I’ll do my best :slight_smile:

jSherz,

Thank you for your reply. last night, i played around with the code, and i came up with this code after lots of trial and error:

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

that seemed to work perfectly.

if you don’t mind, can you help me with another problem? i don’t want the answer, but maybe you can point me in the right direction. the problem is:

[code]Create a text box that will allow a user to input a job qualification and receive a “Yes” or
“No” answer about whether you have that qualification.

  • You must use an array to store your list of qualifications[/code]

I’ve been reading up on arrays, but i don’t know how to call the information from that array and then to echo the “yes” or “no”.

any help would be appreciated.

Thanks!

First of all, define the array. Set a variable to store the result of our test - failed our found. Then loop through the qualifications. If you find the qualification, set the variable to true.

In pseudo-code style.

[code]quals = ‘some qual’, ‘another qual’, ‘PHP Pro’
found = false
looking for = ‘another qual’

loop quals
if item in quals is ‘some qual’ then
found = true
end if
end loop

if found then

output ‘yes’

else

output ‘no’

end if[/code]

Arrays: http://php.net/manual/en/language.types.array.php

Looping: http://php.net/manual/en/control-structures.foreach.php
OR
http://php.net/manual/en/control-structures.for.php

here is my new code, but i can’t seem to get it to work. the return is always “no”…

[code]<?php
$qual=array(‘html’,‘web’,‘graphic’,‘SQL’);
?>

<?php if ($qualification==$qual) echo "Yes"; else echo "No"; ?>[/code]

what am i doing wrong?

You have to loop through the values:

[php]$found = false;

foreach($qual as $q_item) {

if($q_item == $qualification) {

$found = true;

}

}

if($found == true) {
echo ‘yes’;
} else {
echo ‘no’;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service