String Manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kapil1089theking
Forum Commoner
Posts: 46
Joined: Wed May 28, 2008 1:51 pm
Location: Kolkata, India
Contact:

String Manipulation

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: String Manipulation

Post by jaoudestudios »

kapil1089theking wrote:If possible write a code for me.
HAHA :lol: Good luck!
Tobey
Forum Newbie
Posts: 12
Joined: Thu May 14, 2009 11:40 am
Location: Germany

Re: String Manipulation

Post 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.
fannnn
Forum Newbie
Posts: 6
Joined: Tue Jun 16, 2009 10:55 am

Re: String Manipulation

Post by fannnn »

str_replace(), preg_match() are your friends.
Post Reply