[英]Word search Replace and Limit specific Character using regex php
我想替换<loc>
和</loc>
之间的特定单词,然后将单词限制为特定数字。
<?php
$string = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://subdomain.example.com</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url>
<url>
<loc>https://subdomain.example.com/s/queen-katwe-2016-720p-hd-480p-hd/</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url><url>
<loc>https://subdomain.example.com/s/justice-league-dark-2017-720p-hd-480p-hd/</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url><url>
<loc>https://subdomain.example.com/s/edge-seventeen-2016-720p-hd-480p-hd/</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url></urlset>';
$search = "/(<loc>)(.*?)(<\/loc>)/";
$replace = mb_strimwidth('$2', 0, 15);
$total = preg_replace($search,$replace,$string);
echo $total;
?>
我已经尝试过,但它不起作用......请帮助我,提前谢谢你
你有 XML 这不仅仅是一个字符串,我建议使用了解 XML 本身的工具,例如DOMDocument
。 我不知道您要执行的具体逻辑是什么,我什至不知道mb_strimwidth
存在,但这可以写成:
$xml = <<<EOT
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://subdomain.example.com</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url>
<url>
<loc>https://subdomain.example.com/s/queen-katwe-2016-720p-hd-480p-hd/</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url><url>
<loc>https://subdomain.example.com/s/justice-league-dark-2017-720p-hd-480p-hd/</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url><url>
<loc>https://subdomain.example.com/s/edge-seventeen-2016-720p-hd-480p-hd/</loc>
<priority>1.0</priority>
<changefreq>always</changefreq>
</url></urlset>
EOT;
$dom = new DOMDocument;
$dom->loadXML($xml);
foreach($dom->getElementsByTagName('loc') as $node) {
if ((XML_ELEMENT_NODE === $node->nodeType) && ('loc' === $node->nodeName)){
$node->nodeValue = mb_strimwidth($node->nodeValue, 0, 15);
}
}
echo $dom->saveHTML();
此处演示: https://3v4l.org/fvS02
注意:您似乎正在使用 URL 做一些事情。 再一次,URL 不仅仅是一个字符串,PHP 有parse_url
用于解析 URL,如果这确实是你正在做的,我鼓励你使用它。
编辑
如果您的源数据不是 XML,我仍然会尽可能使用解析器。 DOMDocument
也支持 HTML,您只需要取消一些警告,因为 HTML 通常不那么严格。
但是,如果您的数据没有解析器,那么使用 RegEx 可能会更好。 为此,我想我想使用回调 function 来确定替换的逻辑。
$xml = <<<EOT
<loc>https://subdomain.example.com</loc>
<loc>https://subdomain.example.com/s/queen-katwe-2016-720p-hd-480p-hd/</loc>
<loc>https://subdomain.example.com/s/justice-league-dark-2017-720p-hd-480p-hd/</loc>
<loc>https://subdomain.example.com/s/edge-seventeen-2016-720p-hd-480p-hd/</loc>
EOT;
var_dump(
preg_replace_callback(
'/<loc>(?<value>[^<]+)<\/loc>/',
static function($matches) {
return sprintf('<loc>%1$s</loc>', mb_strimwidth($matches['value'], 0, 15));
},
$xml
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.