【问题标题】:Replace many code lines in PHP between tags在标签之间替换 PHP 中的许多代码行
【发布时间】:2017-04-20 08:28:17
【问题描述】:

我已经得到了一个页面 php 与这一行:

$url = file_get_contents('http://web.com/rss.php');

现在我想替换这个:

<link>http://web.com/download/45212/lorem-ipsum</link> <link>http://web.com/download/34210/dolor-sit</link> <link>http://web.com/download/78954/consectetur-adipiscing</link> <link>http://web.com/download/77741/laboris-nisi</link>...

有了这个:

<link>http://otherweb.com/get-d/45212</link> <link>http://otherweb.com/get-d/34210</link> <link>http://otherweb.com/get-d/78954</link> <link>http://otherweb.com/get-d/77741</link>...

我已经用str_replace 替换了一部分,但我不知道要替换另一部分。

这是我目前所做的:

$url = str_replace('<link>http://web.com/download/','<link>http://otherweb.com/get-d/', $url);

【问题讨论】:

  • 那么当你运行 str_replace 时你会得到什么结果?
  • 我需要数字。&lt;link&gt;http://otherweb.com/get-d/&lt;/link&gt; &lt;link&gt;http://otherweb.com/get-d/&lt;/link&gt; &lt;link&gt;http://otherweb.com/get-d/&lt;/link&gt; &lt;link&gt;http://otherweb.com/get-d/&lt;/link&gt;...
  • 看起来它会工作。
  • 或者您是否希望它剥离“/45212”之后的信息?

标签: php regex preg-replace preg-match str-replace


【解决方案1】:

您只是缺少一个简单的正则表达式来清理最后一部分。

我是这样做的:

$messed_up = '
<link>http://web.com/download/45212/lorem-ipsum</link>
<link>http://web.com/download/34210/dolor-sit</link>
<link>http://web.com/download/78954/consectetur-adipiscing</link>
<link>http://web.com/download/77741/laboris-nisi</link>';

// Firstly we can clean up the first part (like you did) with str_replace
$clean = str_replace('web.com/download/','otherweb.com/get-d/', $messed_up);

// After that we'll use preg_replace to get rid of the last part
$clean = preg_replace("/(.+\/\d+)\/.*(<.*)/", "$1$2", $clean);

printf($clean);
/* Returns:
<link>http://otherweb.com/get-d/4521</link>
<link>http://otherweb.com/get-d/3421</link>
<link>http://otherweb.com/get-d/7895</link>
<link>http://otherweb.com/get-d/7774</link>
*/

我做的很快,所以可能还有一些改进的余地,但它确实有效。

您可以在实践中查看代码HERE

如果您有兴趣学习 PHP RegEx,This 是一个很好的练习场所。

【讨论】:

  • 你的解决方案很好,但是如果他要替换的新网址包含一个数字,它将被破坏。
  • 你说得对,我写这篇文章时只考虑了这个例子。我已经更新了答案,所以现在更灵活了
【解决方案2】:

您应该用token 替换链接的初始部分,然后preg_replace 搜索第一个/ 并替换为&lt;/link&gt; 的字符串结尾。所以你用你想要的初始部分替换你的令牌。

$url = str_replace('<link>http://web.com/download/','init', $url);
$url = preg_replace("/\/.+/", "</link>", $url);
$url = str_replace('init', '<link>http://otherweb.com/get-d/', $url);

【讨论】:

    【解决方案3】:

    你可以用一行正则表达式完成这一切:)

    正则表达式

    下面的正则表达式将检测您的中间编号部分....

    <link>http:\/\/web\.com\/download\/(.*?)\/.*?<\/link>
    

    PHP

    要在 PHP 中使用它,你可以使用这行代码

    $url = preg_replace("/<link>http:\/\/web\.com\/download\/(.*?)\/.*?<\/link>/m", "<link>http://otherweb.com/get-d/$1</link>", $url);
    

    这应该完全符合您的需要!

    说明

    它的工作方式是preg_replace 在开头查找&lt;link&gt;http://web.com/download/,在结尾查找/{something}&lt;/link&gt;。它将中间区域捕获到$1

    因此,当我们运行 preg_replace ($pattern, $replacement, $subject) 时,我们告诉 PHP 只找到中间部分(您的 URL 中的数字)并将它们嵌入到 "&lt;link&gt;http://otherweb.com/get-d/$1&lt;/link&gt;" 中。

    我测试了它,它似乎可以工作:)

    编辑:我会为你推荐这个答案,因为它只需要一行,并且不需要任何str_replace。即使中间部分是字母数字,我的答案也将起作用,而不仅仅是数字。

    【讨论】:

    • 不错,但@justarustyspoon 解决方案仍然更好,因为它只从链接中捕获所需的信息。想想url是否返回要替换的不同链接。
    • 我知道这一点,但我假设的目的是将一个集合域切换到另一个域,因此进行全面替换可能会超出范围,因为它会阻止提问者对不同域进行不同的匹配稍后。
    【解决方案4】:

    你要做的就是:

    1. 提取相关数据,例如五位数字
    2. 将提取的部分放入新的上下文中
    $input = 'http://web.com/download/45212/lorem-ipsum'; echo preg_replace('/.*\/(\d+).*/', 'http://otherweb.com/get-d/$1', $input);

    要提取相关部分,您可以使用(\d+),这意味着:找到一个或多个数字,括号使其成为匹配组,因此您可以通过$1访问该值。

    要匹配和替换整行,您必须在 (\d+) 部分之前和之后使用 .* (这意味着找到任意数量的任意字符)来扩充模式。

    通过这个设置,整个字符串匹配,所以整个字符串将被替换。

    【讨论】:

    • 很好的解决方案,但就像其他答案一样,如果位数远离 5 或小于 5,它将无法正确捕获
    • 你的正则表达式应该是 ´/.*\/(\d+).*/´ 然后它将捕获任意数量的数字
    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多