How do I replace certain parts of my string?

后端 未结 5 993
孤独总比滥情好
孤独总比滥情好 2020-11-22 04:29

How can I replace a certain part of my string with another one?

Input string:

\"Hello, my name is Santa\"

How can I change all

5条回答
  •  独厮守ぢ
    2020-11-22 05:05

    str_replace is sufficient for simple replacement jobs (such as replacing a single letter), but the use of preg_replace is generally advised (if you want something more flexible or versatile), because it's flexible and versatile. And as the 'a' is just an example...:

    $String = preg_replace('//','',$String);
    

    Or if you want multiple replacements at once:

    $String = preg_replace(array('//','//'),array('',''),$String);
    

    preg_replace can, unfortunately, be quite tricky to use. I recommend the following reads: http://php.net/manual/en/function.preg-replace.php http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html

    Also, if you decide to use str_replace(), and your replacement needs to be case-sensitive, you're going to need str_ireplace().

提交回复
热议问题