php preg_replace用于html标签内的属性 [英] php preg_replace for property inside html tags
问题描述
我的问题是如何在字符串中替换<script>
标记的src
值,如本示例中所示(嗯,在标记属性的更一般情况下,我需要此值):
My problem is how to replace the src
value of a <script>
tag inside a string like in this example (well, I need this in a more general scenario of properties inside tags):
$data = <<<EOD
<script language="javascript" src= "../tests/ajax-navigation.js"></script>
...
<img src="../404.jpg" alt="404">
...
EOD;
我在php中使用了此功能:
I used this function in php:
class Search{
public static function replaceProperty($data, $start, $end, $property, $alias, $limit = -1){
//get blocks formed as: $start $property = "..." $end or $start $property = '...' $end
$pattern = "!(".$start."){1}(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)(".$end."){1}!s";
$data = \preg_replace($pattern, "{$start}\${2}{$property}=\"{$alias}\"\${4}{$end}", $data, $limit);
return $data;
}
}
我这样称呼:
$data = Search::replaceProperty($data, "<script", ">", "src", $alias);
真正奇怪的是,标签<script>
和<img>
都被更改了!
我当然可以这样称呼
What is really strange is that both tags <script>
and <img>
get changed!
Of course I can call it like
$data = Search::replaceProperty($data, "<script", "</script>", "src", $alias);
但这不能回答一般情况!
but this doesn't answer the general case!
只需使用正则表达式来阐明几点:
i.要搜索的实际字符串是:
i. the actual string to search for is:
$data = <<<EOD
<script language="javascript" src= "../tests/ajax-navigation.js"></script>
...
<script language="javascript" type="text/javascript">
...
<img src="../404.jpg" alt="404">
...
EOD;
ii.正则表达式$pattern = "!(".$start."){1}(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)(".$end."){1}!s";
或最简单的形式$pattern = "%".$start."(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)".$end."%s";
(仅3个子模式)按预期标识了第一个<script>
,但是...它占用了第二个<script>
并终止于第一个<img>
的>
更改两者之间找到的任何src
属性!
ii. the regex $pattern = "!(".$start."){1}(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)(".$end."){1}!s";
or in the simplest form $pattern = "%".$start."(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)".$end."%s";
(just 3 subpatterns) identifies the first <script>
as expected but...it takes the second <script>
and terminates at the >
of the first <img>
changing whatever src
property it finds in between!
iii.通过删除模式末尾的s
元字符,导致$pattern = "%".$start."(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)".$end."%";
的行为符合预期,但当使用Enter键破坏标签时失败:
iii. by deleting the s
metacharacter at the end of the pattern resulting in $pattern = "%".$start."(.*?)".$property."\s*=\s*[\"\'](.*?)[\"\'](.*?)".$end."%";
behaves as expected but fails when the tags are broken with enters:
<script language="javascript" src= "../tests/ajax-navigation.js"
></script>
iv.当然,我的意图是替换而不是删除src
属性中的值.
iv. and, of course my intention is to replace and not to delete the value at src
property.
希望这些可以澄清我的问题.
Hope these clarify my question.
推荐答案
正如我所说,我将使用DOMDocument()
,但这是使用正则表达式的答案:
As I said I'll use DOMDocument()
but here is an answer with regex:
class Search{
public function __construct(){}
public static function replaceProperty($data, $tag, $property, $alias, $limit = -1){
//get blocks formed as: <$tag...$property=["|']...["|']...[/>|>]
$pattern = '%<\s*'.$tag.'(\s+(\w+)(\s*\=\s*(\'|"|)(.*?)\\4\s*)?)*\s*(\/>|>)%s';
$result = \preg_match_all($pattern, $data, $matches, PREG_PATTERN_ORDER);
if(!empty($result)){
$search = array();
$replace = array();
//found them at index = 0!
foreach($matches[0] as $i=>$found){
if(($limit >= 0) && ($i >= $limit))
break;
if(isset($matches[2]) && isset($matches[5]) && $matches[2][$i] == $property){
$search[] = $found;
$replace[] = \str_replace($matches[5][$i], $alias, $found);
}
}
$data = \str_replace($search, $replace, $data);
}
return $data;
}
}
并这样称呼:
$data = Search::replaceProperty($data, "script", "src", $alias);
我从使用了Emanuele Del Grande的答案.此帖子,可能是此!
谢谢.
I used Emanuele Del Grande's answer from this post which might is a reproduction of posts like this!
Thanks.
这篇关于php preg_replace用于html标签内的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!