/    Sign up×
Community /Pin to ProfileBookmark

str replace apostrophe

`function replace_text($text) {
$text = str_replace(“I’m”, “my name”, $text);
return $text;
}
add_filter(‘the_content’, ‘replace_text’);`

Hi, I have this function to replace all “I’m” with “my name”. Except that “I’m” contains the apostrophe. This makes the function not working. can you help me?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 27.2020 — The function seems to work fine:
<i>
</i>$ /usr/bin/php -a
Interactive shell

php &gt; function replace_text($text) {
php { $text = str_replace("I'm", "my name", $text);
php { return $text;
php { }
php &gt; echo replace_text("testing I'm testing");
testing my name testing
php &gt;

Are you sure the text being processed has a "'" character, or could it be a "directional" quote or even a HTML entity?
Copy linkTweet thisAlerts:
@mira2000authorApr 28.2020 — @NogDog#1617887 Thank you for your answer. In fact using "echo" seems to work, but if you actually text it on a web page it doesn't work, it doesn't replace anything. By removing the apostrophe instead (for example using "I am" instead of "I'm"), it works perfectly.
Copy linkTweet thisAlerts:
@mira2000authorApr 28.2020 — P:S. I use php 7.3
Copy linkTweet thisAlerts:
@NogDogApr 28.2020 — Looks like add_filter() is a WordPress function? (https://developer.wordpress.org/reference/functions/add_filter/) I've not done anything with WP in years, and have no idea what it's use case is, but maybe you could search on it and problems with apostrophes? (Sorry I can't be more help. :( )
×

Success!

Help @mira2000 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...