MadnessRed Posted March 11, 2008 Share Posted March 11, 2008 Hi, what i want to do is replace part of an entry. For example... Item bought was c was entered the output would be The last product bought wasc and if if wasn't c, say it was a that too would be possible. thi sis jsut an example, but is it possible to do? Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/ Share on other sites More sharing options...
Altair Posted March 11, 2008 Share Posted March 11, 2008 I'm not sure I totally understand what you are trying to do, but perhaps something like.. <?php $lastPurch = strlen($c) ? $c : $a; echo 'Last Product Purchased Was {$lastPurch}'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-489905 Share on other sites More sharing options...
MadnessRed Posted March 12, 2008 Author Share Posted March 12, 2008 no basically Ill try an illustrate what i mean <textarea> <input type=button value=Convert> <textarea> So when you type in "Item bough was c" into the top one it would be expecting the "Item bought was" and replace that bit with "You purchased item" Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-490743 Share on other sites More sharing options...
Altair Posted March 13, 2008 Share Posted March 13, 2008 no basically Ill try an illustrate what i mean <textarea> <input type=button value=Convert> <textarea> So when you type in "Item bough was c" into the top one it would be expecting the "Item bought was" and replace that bit with "You purchased item" You mean something like: <textarea name="unconverted">Item bought was c</textarea> <input type="button" value="Convert" /> <textarea name="converted"><?php echo str_ireplace('Item bought was', 'You purchased item', $_POST['unconverted']); ?></textarea> Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-491059 Share on other sites More sharing options...
MadnessRed Posted March 13, 2008 Author Share Posted March 13, 2008 yes thats exactly what i want. Thanks also can i add variables. For example <?php echo str_ireplace('Item * was', 'You purchased item', $_POST['unconverted']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-491499 Share on other sites More sharing options...
MadnessRed Posted March 13, 2008 Author Share Posted March 13, 2008 also say the items were numbered could I so something for if the number was < 3 eg if (product<3) echo (Your produtr is over £5.00) else echo (your product is under £5.00 Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-491508 Share on other sites More sharing options...
MadnessRed Posted March 13, 2008 Author Share Posted March 13, 2008 sorry about multiple psoting, cant find an edit button. Anyway also can i delete line, say i wanted to delete line 2 to 4 could I do that and say I wanted to delete from line 6 to the first instance of the word "The" is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-491590 Share on other sites More sharing options...
Altair Posted March 14, 2008 Share Posted March 14, 2008 also can i add variables. <?php echo str_ireplace('Item * was', 'You purchased item', $_POST['unconverted']); ?> Yes... If you are talking about multiple variables within the string, it would be a bit more complicated. I would recommend reviewing this method at http://us3.php.net/str_ireplace. You can find a great deal in google by doing a search for "php manual string functions" (no quotes). That should help with your string parsing endeavors also say the items were numbered could I so something for if the number was < 3 I'm assuming you mean like a quantity purchased? Yes, you could do it. however, if you mean parsing this data out of a string, it could be considerably more complex... You know much about regular expressions (regex)? also can i delete line, say i wanted to delete line 2 to 4 could I do that Again... Yes. This can be done. What I would do here is probably use regex to break the string into an array which contains each line. and say I wanted to delete from line 6 to the first instance of the word "The" Yes, this can be done but it would require a bit more creative thinking. I would love to provide you with example code to take care of these questions, but some things are better learned if they are discovered I know... I always hated that comment too... My advice to you is to spend a great deal of time at php.net. A helpful tip... When doing a search in google for php help, begin the search with "php manual [string functions, array functions, etc...]" (no quotes or brackets []). This almost always bring php.net up as the first result. Please note: Whenever you ask me if something is possible in a programming language. I will 99.999999999999% of the time say, "yes". It's just a matter of thinking about it correctly. The language may not always handle it very well, but there is almost nothing that can't be done Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-492015 Share on other sites More sharing options...
MadnessRed Posted March 14, 2008 Author Share Posted March 14, 2008 lol can i make fake £50 quid noted in php but yh, ok, thnks Ill try that Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-492367 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 sorry about multiple psoting, cant find an edit button. Anyway also can i delete line, say i wanted to delete line 2 to 4 could I do that and say I wanted to delete from line 6 to the first instance of the word "The" is that possible? jost for start<?php $test = '1 2 3 4 5 6 7 8 some bla Thebla xxx 9 The 2nd 10'; echo "remove line 2 - 4 \n<br /><pre>"; echo preg_replace('/((\A[^\n]*\n){1})([^\n]*\n){3}/','$1',$test); echo "</pre>\n<hr />\n"; echo "remove from line 6 to 1st 'The' \n<br /><pre>"; echo preg_replace('/(\A([^\n]*\n){5,5}).*?((\bThe\b)|(\Z))/s','$1$3',$test); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-492561 Share on other sites More sharing options...
MadnessRed Posted March 15, 2008 Author Share Posted March 15, 2008 thanks Quote Link to comment https://forums.phpfreaks.com/topic/95684-replace-part-of-a-text-entry/#findComment-492849 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.