cobusbo Posted March 1, 2015 Share Posted March 1, 2015 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 https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/ Share on other sites More sharing options...
ginerjm Posted March 1, 2015 Share Posted March 1, 2015 if (false !== stripos(($string,$word)) $string = $newstring; ]/php] Link to comment https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507160 Share on other sites More sharing options...
cobusbo Posted March 1, 2015 Author Share Posted March 1, 2015 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 https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507166 Share on other sites More sharing options...
Ch0cu3r Posted March 1, 2015 Share Posted March 1, 2015 because an array don't work It will if you use a for(each)/while loop? Link to comment https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507168 Share on other sites More sharing options...
ginerjm Posted March 1, 2015 Share Posted March 1, 2015 $badnames = ('Admin','Admin2','Admin3'); $name2 = 'Nice try'; $name = ...; foreach($badnames as $word) { if (false !== stripos($name,$word)) { $name = $name2; break; } } Link to comment https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507169 Share on other sites More sharing options...
CroNiX Posted March 1, 2015 Share Posted March 1, 2015 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 https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507170 Share on other sites More sharing options...
cobusbo Posted March 1, 2015 Author Share Posted March 1, 2015 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 https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507171 Share on other sites More sharing options...
CroNiX Posted March 1, 2015 Share Posted March 1, 2015 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 https://forums.phpfreaks.com/topic/294988-string-if-a-specific-word-is-found-in-the-string-replace-the-whole-string/#findComment-1507173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.