【问题标题】:Alter urls within a tag更改标签内的网址
【发布时间】:2013-08-11 13:47:17
【问题描述】:

我遇到了一个菜鸟墙,我不确定如何克服它。

当显示来自数据库的某些内容时,该内容将包含 HTML 标记。 其中一个标签是<a> 链接。

它的 href 将等于以下任何一个。

http://www.example.com
http://www.example.com/
http://www.example.com/some/other/stuff
/some/other/stuff
/
www.example.com
www.example.com/

我需要做的,我已经尝试了使用 str_replace() 的逻辑,但我无法让它 100% 正常工作......将上述所有链接转至此。

http://www.example.com/2012_2013
http://www.example.com/2012_2013/
/2012_2013/some/other/stuff
/2012_2013
www.example.com/2012_2013
www.example.com/2012_2013/

我的问题主要是转弯

/some/other/stuff

进入

/2012_2013/some/other/stuff

当我不知道/this/could/be 是什么时,我该如何找到它并在前面加上/2012_2013

这似乎不是 100% 工作

$content = str_replace("http://www.example.com/","http://www.example.com/2012_2013/",$wData['field_id_2']);                                     
$content = str_replace('href="/"','href="/2012_2013/"',$content);
echo $content;

提前致谢。

【问题讨论】:

  • 这些文件夹实际上存在于您的系统中,对吧?
  • 不,它们是其他页面的 URL。
  • php.net/parse_url 是你的朋友。当 PHP 已经为您完成所有这些时,不要尝试构建您自己的正则表达式。再加上 DOM 来实际找到 <a> 标签并获取它们的 src,你就会被设置。
  • 谢谢,我会看看 parse_url。需要学习的东西:)。
  • 请为此问题选择一个答案。

标签: php regex hyperlink str-replace


【解决方案1】:

parse_url 函数的帮助下,下面的代码应该适合你。

$arr = array('http://www.example.com', 'http://www.example.com/',
'http://www.example.com/some/other/stuff', '/some/other/stuff',
'/some/other/stuff/', '/2012_2013/some/other/stuff', '/', 'www.example.com',
'www.example.com/');

$ret = array();
foreach ($arr as $a) { 
   if ($a[0] != '/' && !preg_match('#^https?://#i', $a))
      $a = 'http://' . $a;
   $url = parse_url ($a);
   $path = '';
   if (isset($url['path']))
      $path = $url['path'];
   $path = preg_replace('#^((?!/2012_2013/).*?)(/?)$#', '/2012_2013$1$2', $path );
   $out= '';
   if (isset($url['scheme'])) {
      $out .= $url['scheme'] . '://';
      if (isset($url['host']))
         $out .= $url['host'];
   }
   $out .= $path;
   $ret[] = $out; 
}

print_r($ret);

输出:

Array
(
    [0] => http://www.example.com/2012_2013
    [1] => http://www.example.com/2012_2013/
    [2] => http://www.example.com/2012_2013/some/other/stuff
    [3] => /2012_2013/some/other/stuff
    [4] => /2012_2013/some/other/stuff/
    [5] => /2012_2013/some/other/stuff
    [6] => /2012_2013/
    [7] => http://www.example.com/2012_2013
    [8] => http://www.example.com/2012_2013/
)

【讨论】:

  • 假设我可以直接提供网址,这看起来很棒。不幸的是,我将拥有一整块文本和 html,我将无法从中提取链接。这就是我尝试 str_replace(); 的原因。但是 parse_url 的一个很好的例子。谢谢!
  • 虽然我理解您的要求,但在 HTML 块中检测 //some/other/stuff 等作为 URL 几乎是不可能的。
  • 我认为这并不容易。我想我会尽可能接近 100%。谢谢!
【解决方案2】:

我会简单地将explode / 并在正确的位置附加2012_2013,然后implode 数组。

所以是这样的:

$link = '<a href="http://www.example.com/some/other/stuff">http://www.example.com/some/other/stuff</a>';

$linkParts = explode('/', $link);
$linkParts[2] = $linkParts[2] . '/2012_2013';
$linkParts[7] = $linkParts[7] . '/2012_2013';

$finalLink = implode('/', $linkParts);

echo $finalLink;

根据以上内容,我假设您的域格式没有改变。

这看起来像是数据库内容问题。最好在您的数据库中正确更新它们,并且您不需要摆弄输出。

【讨论】:

  • 不幸的是,有太多人在处理内容,并且始终保持统一几乎是不可能的。但是爆炸的想法听起来很棒。我会试一试。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2019-11-29
  • 2018-04-04
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多