正则表达式替换的PHP子串

时间:2014-04-11 18:53:27

标签: php regex

我有以下正则表达式:

 $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">$1</A>",$string);

用它来解析这个字符串:http://www.ttt.com.ar/hello_world

生成这个新字符串:

<a target="_blank" href="http://www.ttt.com.ar/hello_world">http://www.ttt.com.ar/hello_world</A>
到目前为止,太好了。我想要做的是让替换$ 1成为$ 1的子串,产生如下输出:

<a target="_blank" href="http://www.ttt.com.ar/hello_world">http://www.ttt.com.ar/...</A>

我的意思是伪代码:

 $string = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a target=\"_blank\" href=\"$1\">substring($1,0,24)..</A>",$string);

这甚至可能吗?可能我只是做错了:)

提前致谢。

3 个答案:

答案 0 :(得分:2)

你表现得不好。不应该使用正则表达式来解析或修改应用程序上下文中的xml内容。

:建议:

  • 使用DOM解析来读取和修改值
  • 使用parse_url()获取协议+域名

示例:

$doc = new DOMDocument();
$doc->loadHTML(
    '<a target="_blank" href="http://www.ttt.com.ar/hello_world">http://www.ttt.com.ar/hello_world</A>'#
);

$link = $doc->getElementsByTagName('a')->item(0);
$url = parse_url($link->nodeValue);

$link->nodeValue = $url['scheme'] . '://' . $url['host'] . '/...';

echo $doc->saveHTML();

答案 1 :(得分:2)

结帐preg_replace_callback()

$string = 'http://www.ttt.com.ar/hello_world';

$string = preg_replace_callback(
    "/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i",
    function($matches) {
        $link = $matches[1];
        $substring = substr($link, 0, 24) . '..';
        return "<a target=\"_blank\" href=\"$link\">$substring</a>";
    },
    $string
);

var_dump($string);
// <a target="_blank" href="http://www.ttt.com.ar/hello_world">http://www.ttt.com.ar/...</a>

注意,您还可以使用PHP中的e修饰符来执行preg_replace()中的函数。这在PHP 5.5.0中已被弃用,有利于preg_replace_callback()

答案 2 :(得分:2)

您可以在这样的前瞻中使用捕获组:

preg_replace(
    "/((?=(.{24}))[\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i",
    "<a target=\"_blank\" href=\"$1\">$2..</A>",
     $string);

这将捕获组1中的整个URL,但它也会捕获组2中的前24个字符。

相关问题