Page 1 of 1

String Manipulation

Posted: Tue Jun 16, 2009 8:17 am
by kapil1089theking
Hi there,
I have two objectives:
1.I am trying to write the best code preferably a function for ignoring a particular word/strings from a given text
for Eg.:
Input: Why are you going there
say I have called the method to Ignore "are" I'll get output as
Output: Why you going there?
2. I want to check wether a particular pattern is present in the text or not?

I may need both of above at the same time or any one of them in single.

If possible write a code for me.

Re: String Manipulation

Posted: Tue Jun 16, 2009 9:21 am
by jaoudestudios
kapil1089theking wrote:If possible write a code for me.
HAHA :lol: Good luck!

Re: String Manipulation

Posted: Tue Jun 16, 2009 9:41 am
by Tobey

Code: Select all

<?php
$blacklist = array("are", "is");
$text = "Why are you going there";
 
$words = explode(" ", $text);
$output = array();
 
foreach($words as $word)
{
  if(!in_array($word, $blacklist)) $output[] = $word;
}
 
$output = implode(" ", $output);
?>
 
I don't know if this code works, I just wrote it down, but yeah, that's one way how you could do it.

Re: String Manipulation

Posted: Tue Jun 16, 2009 11:44 am
by fannnn
str_replace(), preg_match() are your friends.