Jump to content

How To Distinguish Words In Text Box?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
tradingjamie

tradingjamie

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
Hi Guys,

Does anybody know how to distinguish words or letters in a text box using php?

For instance - I have a text box, where people can type text in (obviously!)... however, if somebody types in a particular word (say "dog") I want to replace it with the word "cat".

Does anybody know hoW to do this?

Thanks!

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
You can use str_replace and do something like:
str_replace("dog", "cat", "The quick brown fox jumped over the lazy dog.");
If you need to match more complex expressions you might want to look into preg_replace()

#3
tradingjamie

tradingjamie

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
Hi John,

Thanks for your reply.

As it goes I am looking to do something a little more complex that Im not sure how to do - if I can get your help on this please I would be really grateful!

Basically I have a page where I have two text boxes - lets call them box A and box B.

The idea is that in box A the user can type in full sentences and then in box B they can type in additional words to insert into the sentence in box A...

The user types a sentence in box A - for example let say they type the sentence "banking has become a bad profession"

What I am trying to make happen is 2 things:

1) when a user highlights a word in box A, this word appears in box B. For example, if the user highlights "bad" then the word bad appears in box B.

Now in box B, when the cursor is at the end of the word bad and they press enter, they can then type in another word… at this stage this is when I want the second thing to happen:

2) After pressing enter on the word in box B, the word highlighted in box A adds curly brackets around it. So in this example it becomes "{bad}". Then as the user types in another word in box B - for example lets say "terrible" - then the highlighted text in box A becomes "{bad|terrible}"

Hopefully Ive made it clear? Can I actually do this in php or do I need to use Javascript or something else?

Thanks so much for all your help, its much appreciated

(Also can php make this happen in real time?)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
That behavior would require JavaScript, though I'm not sure it can detect highlighting. It does have regular expression search/replace support, however.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
tradingjamie

tradingjamie

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
Hey - thanks for the replies guys!

JavaScript heh....

Does anybody fancy earning a quick $50 to write this code for me? I really have no idea when it comes to coding (as you can probably tell)

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
put it up on the freelance offers...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall