【发布时间】:2012-03-13 14:13:08
【问题描述】:
好的,我知道有数以万亿计的类似问题,但我发现很难做到这一点。 我有一些这种格式的字符串:
$x = '<iframe src="[File:19]"></iframe>';
$y = '<img src=[File:2212] />';
$z = '<source src="[File:42]" />';
我正在尝试获取在 File: 之后给出的 ID,并将整个 [File:xxx] 替换为另一个字符串。我正在尝试如下,但似乎我无法完全理解 preg_replace 的用法。
$file = ('<iframe src="[File:134]"></frame>');
$rex = "/^.*(\[File:[0-9]{1,}\])/i" ;
if ( preg_match($rex, $file, $match) ) {
echo 'OK';
}
$file = preg_replace ($rex, "http://lala.com/la.pdf", $file);
echo "<br>".htmlentities($file)."<br>";
您能否给我一些提示,告诉我如何做到这一点?
提前致谢。
【问题讨论】:
-
您可以使用
preg_match_all并删除^.*..(应该可以)但是实际上您应该为此使用PHP DOM
标签: php regex html-parsing