Does anyone know how to strip a $var of everything except certain characters?
For example say I had "This is a big red ball with a big red stick" in a $string and wanted to remove everything apart from "big red" so that the $string would end up being "big red big red"...... probably not a great example but you catch my drift.
Any ideas?
str_replace() reversed?
Moderator: General Moderators
I don't know of any function that does this - it sounds a little obscure. You'd probably have to write your own algorithm to handle this, maybe using strpos(), recording all the locations of particular phrases and then reconstructing a new string from the gathered data. Or maybe some regex wizardry would help.
< Simon >
< Simon >
Think of this idea...
Make an array with allowed words.
Use explode() on your string using space as delim, and you get all words in a different array.
Loop each word in the second array using [url=http://se.php.net/manual/en/function.in-array.php]in_array() to see if it exists. If it does, keep it, else remove it using array_slice() or unset().
Left you have a string containing allowed words.
Make an array with allowed words.
Use explode() on your string using space as delim, and you get all words in a different array.
Loop each word in the second array using [url=http://se.php.net/manual/en/function.in-array.php]in_array() to see if it exists. If it does, keep it, else remove it using array_slice() or unset().
Left you have a string containing allowed words.