Jump to content

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


cobusbo
Go to solution Solved by ginerjm,

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

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.