malikye Posted December 19, 2011 Share Posted December 19, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/ Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299170 Share on other sites More sharing options...
malikye Posted December 19, 2011 Author Share Posted December 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299196 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 You'll find it easier to use preg_replace, which uses regular expressions. Try this out: $str = 'underline the first e'; $str = preg_replace('/e/', '<u>e</u>', $str, 1); Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299205 Share on other sites More sharing options...
malikye Posted December 19, 2011 Author Share Posted December 19, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299224 Share on other sites More sharing options...
malikye Posted December 19, 2011 Author Share Posted December 19, 2011 is there anyway i can get both those codes to work for $fname at once without another line of code? geez, i hope that question made sense. Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299232 Share on other sites More sharing options...
malikye Posted December 19, 2011 Author Share Posted December 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299241 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299242 Share on other sites More sharing options...
malikye Posted December 19, 2011 Author Share Posted December 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299245 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253456-editing-a-form-after-submitting/#findComment-1299248 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.