/    Sign up×
Community /Pin to ProfileBookmark

PHP str_replace for only text, not url or links

Hi I’m using this code to replace all the words “Oil” on my blog pages with the words “Olive”
`
function replace_text($text) {
$text = str_replace(‘oil’, ‘olive’, $text);
return $text;
}
add_filter(‘the_content’, ‘replace_text’);`

The problem is that internal links containing the word “Oil” are also replaced, generating invalid links, 404. I would need a function that recognizes the links, and replaces only the text, not the links. The code posted works well. Except that if I have a link inside the page like https://oil-italy.com, it becomes https://olive-italy.com. The code must work for text only, and not for links. Can you help me?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumApr 27.2020 — This solution comes across my mind:
  • - Find the links via regex and replace them by a fixed text, e. g. {{alink}}. Save the replaced urls.

  • - Now perform your replacement.

  • - Finally replace the placeholders {{alink}} by their original values having been saved before.

    I'm curious if anyone finds a solution more simple.
  • Copy linkTweet thisAlerts:
    @mira2000authorApr 27.2020 — Could you give me an example with the code?

    Copy linkTweet thisAlerts:
    @NogDogApr 27.2020 — Maybe (untested):
    <i>
    </i>$text = preg_replace('/(&gt;[^&lt;]*)boilb([^&lt;]*&lt;)/', "$1olive$2, $text);

    The idea is to only modify it if it's between the end of a HTML tag and the start of another tag.
    Copy linkTweet thisAlerts:
    @mira2000authorApr 27.2020 — how do I position it compared to my original code?
    Copy linkTweet thisAlerts:
    @mira2000authorApr 27.2020 — It works, thanks. Change the anchor text but not the permalink. Great.
    Copy linkTweet thisAlerts:
    @mira2000authorApr 29.2020 — @NogDog#1617879 Thanks so much. You know instead of "oil", you want to put "oil"? Would that be correct?

    $text = preg_replace("/(&gt;[^&lt;]*)bl'oilb([^&lt;]*&lt;)/", "$1olive$2, $text);
    Copy linkTweet thisAlerts:
    @NogDogApr 29.2020 — > @mira2000#1617936 instead of "oil", you want to put "oil"?

    I think we need a clearer explanation? That doesn't makes sense to me (sounds like you are saying you want to replace "oil" with "oil").
    Copy linkTweet thisAlerts:
    @mira2000authorApr 29.2020 — @NogDog#1617956 You're right I wrote wrong. If the word to be replaced was not "oil", but "l'oil", with the apostrophe? I noticed that apart from the apostrophe I also have difficulty with words with a dot (such as "oil.olive"). In this case the replacement does not work.
    Copy linkTweet thisAlerts:
    @NogDogApr 29.2020 — "." is a special character in regular expressions that means "any character", so you would need to escape it with a back-slash in that case: /(&gt;[^&lt;]*)bolive.oilb([^&lt;]*&lt;)/. This single quote should be fine as is (it has no special regex meaning).
    Copy linkTweet thisAlerts:
    @mira2000authorApr 30.2020 — @NogDog#1617963 Sorry if I bother you again, but you're the only one who answers me, then I won't bother you anymore. This code works:

    $text = preg_replace('/(&gt;[^&lt;]*)boilb([^&lt;]*&lt;)/', "$1olive$2, $text);

    In fact, now the urls are not replaced. The problem is however that if I have 2 words, one of which is normal, the other in bold (for example red **house**) and and if I wanted to replace "red house" with "home", the code doesn't work, because part of the word is outside of <>. In fact, the html code would be red <strong>house <strong>. Can you help me this last time ???
    Copy linkTweet thisAlerts:
    @NogDogApr 30.2020 — That makes things trickier. You can explicitly look for it and replace it, I suppose:
    ``<i>
    </i>preg_replace('/(&gt;[^&lt;]*)breds&lt;strong&gt;houseb.*&lt;/strong&gt;([^&lt;]*&lt;)/', "$1home$2, $text)<i>
    </i>
    ``

    But that's getting really messy (and no guarantee I didn't screw something up there). That would get rid of the "strong" tag in this case and just have the word "home" by itself -- I hope.
    Copy linkTweet thisAlerts:
    @garima2zlatoApr 30.2020 — Really Nice Post Admin, Very helpful looking for more posts, Now I have to share some information about "ATT Email Login” Problem. If you want to fix this issue. You can simply visit: https://bit.ly/35ljYNa
    Copy linkTweet thisAlerts:
    @mira2000authorApr 30.2020 — @NogDog#1618005 I thank you so much. Unfortunately it doesn't work. All the text disappears. But thank you very much. You have helped me a lot
    ×

    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 6.2,
    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: @meenaratha,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...