How to remove unwated code from a php string?
I want replace text in a st开发者_StackOverflow社区ring using php in-built function, How do I accomplish this?
Use one of:
str_replace('look', 'replace', $context);
preg_replace('pattern', 'replace', $context);
I think you are looking for this example
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
More details here
with regards
Wazzy
It depends on what text you want to replace, if you just want a very simple replace, you might find some success in the str_ireplace()
function.
Of course without having an example of what it is exactly you want to remove, we can't give you a better answer. More complicated examples might require you to write a regular expression, and then use a string-replacement method that makes use of regular expressions like preg_replace()
.
Your question title seems to suggest that you are interested in sanitizing a string. If this is the case, you could consider htmlspecialchars()
if you wish to prevent HTML code from being rendered as such. If you are interested in sanitizing for database input, you might wish to consider mysql_real_escape_string()
for a MySQL query.
Otherwise, as mentioned you can use str_replace()
(or the case-insensitive version) to replace all instances of one string with another string.
精彩评论