Jump to content

String if a specific word is found in the string replace the whole string


cobusbo

Recommended Posts

Hi I'm currently struggling with a problem with my string

Lets say for example I got a sentence like

 

"This is my shoes"

 

and I want to check for the word shoes in that string and if it is within the string I want it to replace the whole string with another string

 

"That was your shoes"

 

is there a way to do this?

I tried preg_replace but it only replace the word shoes not the whole string.

if (false !== stripos(($string,$word))

$string = $newstring;

]/php]

 Ok here is my code

$badnames = 'Admin';
$name = urldecode($_SERVER['HTTP_X_MXIT_NICK']);
$name2 = "Nice Try";
$test = stripos($name,$badnames);
if ($test !== false){
$name = $name2;

My question now is how would I go if I would like to add more words because an array don't work

Do you want to match actual WORDS? Or just see if a string is located within a string? If it's the first one, you don't want to use stripos() as it will do the 2nd one.

 

Like if you want to search for "admin", stripos() would find it in:

"let's readminister the medicine"

"administrative assistant"

etc.

It will if you use a for(each)/while loop?

 

 

$badnames = ('Admin','Admin2','Admin3');
$name2 = 'Nice try';
$name = ...;
foreach($badnames as $word)
{
if (false !== stripos($name,$word))
{
$name = $name2;
break;
}
}

Ah thank you

 

Do you want to match actual WORDS? Or just see if a string is located within a string? If it's the first one, you don't want to use stripos() as it will do the 2nd one.

 

Like if you want to search for "admin", stripos() would find it in:

"let's readminister the medicine"

"administrative assistant"

etc.

Yes I'm aware of it, Basically I want to use this option to block users from using any name with the specific words inside their name. 

and I want to check for the word shoes in that string and if it is within the string I want it to replace the whole string with another string

Good deal, just wasn't clear as you asked about a "word" in the string, not a "string" in the string.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.