Question about preg_replace
-
I am writing a function to filter post content that will perform a regex substitution using preg_replace. My end goal is to find any YouTube URLs in a post and add parameters to them. If the parameters are already there, then they should not be added again.
When I test the regex, it works the way I want. This test demonstrates that the replacement is made when needed…
https://regex101.com/r/kS1wU3/1
… while this test shows that the replacement is NOT made when it ISN’T needed; that is, when the added parameters are already there.
https://regex101.com/r/kS1wU3/2
However, when I use this regex in my function, the replacement isn’t made. If I remove the newline from the end of the regex, then the parameters are added even if they are already present, and the URL gets longer with each save.
Can anyone help with this? Or is there an entirely different way to alter YouTube parameters that doesn’t involve preg_replace at all?
- The topic ‘Question about preg_replace’ is closed to new replies.