• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: str_replace doesn’t replace accurately

Resolved: str_replace doesn’t replace accurately

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am working on a multilingual wordpress website where I need to append a loclale in all internal url in a web page.
So I have all of the content of the webpage in the $content variable. now
now Ideally this function should replace those to those urls which dosen;t have a locale in them. but it is appending locale in all the url, however the $arr has only those urls which needs to be appended with a locale but my str_replace is appending all urls that we have in matches[2] array.

Answer:

When you replace www.example.com with www.example.com/us, it will replace it anywhere it appears, even if there’s already /us after it.
You can use a regular expression with a negative lookahead to replace a string only if it’s not followed by some other pattern.
The regular expression matches each URL unless it’s followed by /us/, and will replace it with $new_url.

If you have better answer, please add a comment about this, thank you!

php str-replace
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to separate multiple answers in one column, for multiple columns, by creating extra columns

26/03/2023

Resolved: How to set consistent decimal separators in R data frame?

26/03/2023

Resolved: How to resolve LNK2001 in c++ projects

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.