我尝试搜索相关答案,但找不到适合我特定需求的内容。我的一个 WordPress 网站上有相当多的 1,000 篇文章内的附属链接 - 它们都以相同的 url 格式和子域结构开头:
http://affiliateprogram.affiliates.com/
但是,在初始 url 格式之后,查询字符串会为每个单独的 url 附加更改以便将访问者引导至目标网站上的特定页面。
我正在寻找能够扫描 html 代码字符串(文章正文)以查找包含上述特定域的所有 href 链接,然后用我选择的另一个标准链接替换整个链接(无论附加什么查询字符串)的东西。
href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination"
被替换为
href="http://www.mylink.com"
我理想地希望通过 php 来做到这一点,因为我有基本的掌握,但如果您有任何其他建议,我将不胜感激所有的意见。
提前致谢。
I have tried searching through related answers but can't quite find something that is suitable for my specific needs. I have quite a few affiliate links within 1,000s of articles on one of my wordpress sites - which all start with the same url format and sub-domain structure:
http://affiliateprogram.affiliates.com/
However, after the initial url format, the query string appended changes for each individual url in order to send visitors to specific pages on the destination site.
I am looking for something that will scan a string of html code (the article body) for all href links that include the specific domain above and then replace THE WHOLE LINK (whatever the query string appended) with another standard link of my choice.
href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination"
gets replaced with
href="http://www.mylink.com"
I would ideally like to do this via php as I have a basic grasp, but if you have any other suggestions I would appreciate all input.
Thanks in advance.
发布评论
评论(4)
http://ideone.com/qaEEM
http://ideone.com/qaEEM
使用正则表达式,例如:
输出
Use a regular expression such as:
output
这非常简单,因为查询字符串只需要一个占位符。
.*?
通常会这样做,但您可以通过匹配任何非双引号的内容来使其更具体:人们可能会过来并推荐冗长的 domdocument 方法,但这对于这样的任务来说可能有点过分了。
That's quite simple, as you only need a single placeholder for the querystring.
.*?
would normally do, but you can make it more specific by matching anything that's not a double quote:People will probably come around and recomend a longwinded domdocument approach, but that's likely overkill for such a task.