Jump to content

replace part of a text entry


MadnessRed

Recommended Posts

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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>';
?>

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.