Jump to content

Adding a prefix/suffix to one or more words in a string


Bladescope

Recommended Posts

Hey PHPFreaks again, I have an array of keywords and a string. What I would like to know is how to create a function for the following:

 

• to search for each of the keywords in the string, no matter the case

• if a keyword/keywords is/are found within the string, to add a prefix and a suffix to the word/s whilst still maintaining the case of the word.

• to return the string with all of the words replaced

 

I wouldn't ask for a full script, but any help would be greatly appreciated.

 

Thanks in advance,

Bladescope

 

 

i believe a this will do what you need if you place it in a loop

 

if (stristr($string, $searchforme)) {
   str_replace($string, substr ( $string  , (stristr($string, $searchforme)), 0 ), substr ( $string  , (stristr($string, $searchforme)), 0 )+strlen($prefix) );
}

 

or for suffix

 

if (stristr($string, $searchforme)) {
   str_replace($string, substr ( $string  , (stristr($string, $searchforme)), 0 )+strlen($searchforme), substr ( $string  , (stristr($string, $searchforme)), 0 )+strlen($suffix) );
}

 

obviously replace the variables with your variables, and you may still need to modify the code, this is untested

 

hopefully this at least gives you the nudge you need :)

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.