如何使用 preg_replace 枚举项目?

发布于 2024-10-05 05:12:13 字数 1401 浏览 4 评论 0原文

我需要在文档图像之前和之后添加一些标签并立即对它们进行编号。 HTML 文档代码是:

....

<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br> 
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br> 

我需要这样的结果代码

<div><a name="#pic1"></a><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></div>
<div><a name="#pic2"></a><img src="http://img.example.com/img/image72.jpg" alt="sometile"> </div> 
<div><a name="#pic3"></a><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> </div> 
<div><a name="#pic4"></a><img src="http://img.example.com/img/image.jpg" alt="sometile"> </div> 
<div><a name="#pic5"></a><img src="http://img.example.com/img/imgger.gif" alt="sometile"> </div> 
<div><a name="#pic6"></a><img src="http://img.example.com/img/somepic.png" alt="sometile"> </div> 

I need to add some tags before and after images on document and numerate them at once.
HTML Document code is:

....

<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br> 
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br> 

I need in result code like this

<div><a name="#pic1"></a><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></div>
<div><a name="#pic2"></a><img src="http://img.example.com/img/image72.jpg" alt="sometile"> </div> 
<div><a name="#pic3"></a><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> </div> 
<div><a name="#pic4"></a><img src="http://img.example.com/img/image.jpg" alt="sometile"> </div> 
<div><a name="#pic5"></a><img src="http://img.example.com/img/imgger.gif" alt="sometile"> </div> 
<div><a name="#pic6"></a><img src="http://img.example.com/img/somepic.png" alt="sometile"> </div> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-10-12 05:12:13

像往常一样,您也可以使用 DOM 来做到这一点:

$counter = 0;
$doc = new DOMDocument();
$doc->loadHTML($str);
foreach ($doc->getElementsByTagName("img") as $img) {
    $sibling = $img->nextSibling;
    while ($sibling) {
        if ($sibling instanceof DOMElement && $sibling->tagName === 'br') {
            $sibling->parentNode->removeChild($sibling);
        }
        $sibling = $sibling->nextSibling;
    }
    $div = $doc->createElement("div");
    $img->parentNode->replaceChild($div, $img);
    $a = $doc->createElement("a");
    $a->setAttribute("name", "pic" . (++$counter));
    $a->appendChild($img);
    $div->appendChild($a);
}
$str = '';
foreach (simplexml_import_dom($doc->getElementsByTagName("body")->item(0))->children() as $elem) {
    $str .= $elem->asXML();
}

As usually, you can also do that with DOM:

$counter = 0;
$doc = new DOMDocument();
$doc->loadHTML($str);
foreach ($doc->getElementsByTagName("img") as $img) {
    $sibling = $img->nextSibling;
    while ($sibling) {
        if ($sibling instanceof DOMElement && $sibling->tagName === 'br') {
            $sibling->parentNode->removeChild($sibling);
        }
        $sibling = $sibling->nextSibling;
    }
    $div = $doc->createElement("div");
    $img->parentNode->replaceChild($div, $img);
    $a = $doc->createElement("a");
    $a->setAttribute("name", "pic" . (++$counter));
    $a->appendChild($img);
    $div->appendChild($a);
}
$str = '';
foreach (simplexml_import_dom($doc->getElementsByTagName("body")->item(0))->children() as $elem) {
    $str .= $elem->asXML();
}
高冷爸爸 2024-10-12 05:12:13

我不喜欢正则表达式+HTML,但这里是(我刚刚编写了一个简单的正则表达式 - 您可能已经有了一个):

$s = '<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br> 
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>';

$i = 0;
function wrap($s) {
        global $i;
        $i++;
        return sprintf('<div><a name="pic%d">%s</div>', $i, $s);
}

print preg_replace('#(<img [^>]+?>) <br>#e', "wrap('\\1')", $s);

演示

重要的部分是 preg_replace() 中的 e 修饰符。请参阅“使用 'e' 修饰符的示例 #4”。


编辑
正如评论中所指出的,$i 当然不是全局变量的最佳名称。如果这将用作简单的“一次性”转换,那么可能没问题。如果不是,请将名称更改为不会发生冲突的名称。或者将其作为public static放在类中。

另外, preg_replace_callback() 存在。它更适合,尽管我发现将函数名称作为字符串传递并评估 functionName('arg') 几乎同样丑陋:)

I'm not a fan of regex+HTML, but here goes (I just cooked up a simple regex—you probably already have one):

$s = '<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br> 
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>';

$i = 0;
function wrap($s) {
        global $i;
        $i++;
        return sprintf('<div><a name="pic%d">%s</div>', $i, $s);
}

print preg_replace('#(<img [^>]+?>) <br>#e', "wrap('\\1')", $s);

(Demo)

The important part is the e modifier in the preg_replace(). See "Example #4 Using the 'e' modifier."


EDIT
As pointed out in the comments, $i is certainly not the best name for a global variable. If this is going to be used as a simple "one-off" transformation it may be alright. If not, change the name to something that will not conflict. Or put it as a public static in a class.

Also, preg_replace_callback() exists. It's better suited, although I find the passing a function name as a string and evaluating functionName('arg') almost equally ugly :)

余生一个溪 2024-10-12 05:12:13

首先,你不应该使用正则表达式来处理 HTML。

但是,在这种情况下,这很容易,因为您几乎要处理整行:

$code = '<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br>
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>';

function get_num_row($matches) {
    global $last_num;
    return '<div><a name="pic' . (++$last_num) . '">' . trim($matches[1]) . '</a></div>';
}

$last_num = 0;
$code = preg_replace_callback('/^(.*)<br>$/m', 'get_num_row', $code);
echo $code;

输出:

<div><a name="pic1"><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></a></div>
<div><a name="pic2"><img src="http://img.example.com/img/image72.jpg" alt="sometile"></a></div>
<div><a name="pic3"><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"></a></div>
<div><a name="pic4"><img src="http://img.example.com/img/image.jpg" alt="sometile"></a></div>
<div><a name="pic5"><img src="http://img.example.com/img/imgger.gif" alt="sometile"></a></div>
<div><a name="pic6"><img src="http://img.example.com/img/somepic.png" alt="sometile"></a></div>

当然,如果您要使用回调,则需要将 $last_num 设置为 0再次。
如果没有全局或静态变量,就无法做到这一点。但如果您只想使用回调一次,只需将 global $last_num; 更改为 static $last_num = 0; 即可。然后它会永远增加,没有机会重置它——但它不会扰乱你的全局命名空间。

First of all, you are not supposed to use Regexes to deal with HTML.

However, in this case it's easy as you are almost dealing with full lines:

$code = '<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br>
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br>
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>';

function get_num_row($matches) {
    global $last_num;
    return '<div><a name="pic' . (++$last_num) . '">' . trim($matches[1]) . '</a></div>';
}

$last_num = 0;
$code = preg_replace_callback('/^(.*)<br>$/m', 'get_num_row', $code);
echo $code;

Output:

<div><a name="pic1"><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></a></div>
<div><a name="pic2"><img src="http://img.example.com/img/image72.jpg" alt="sometile"></a></div>
<div><a name="pic3"><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"></a></div>
<div><a name="pic4"><img src="http://img.example.com/img/image.jpg" alt="sometile"></a></div>
<div><a name="pic5"><img src="http://img.example.com/img/imgger.gif" alt="sometile"></a></div>
<div><a name="pic6"><img src="http://img.example.com/img/somepic.png" alt="sometile"></a></div>

Of course you need to set $last_num to 0 if you are going to use the callback again.
There's no way to do it without a global or static var. But if you are going to use the callback only once ever you can simply change global $last_num; to static $last_num = 0;. Then it will increment forever with no chance of resetting it - but it won't clutter your global namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文