1

I want replace text in a string using php in-built function, How do I accomplish this?

1

4 Answers 4

2

Use one of:

str_replace('look', 'replace', $context);

preg_replace('pattern', 'replace', $context);

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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().

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.