Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在包含标题标签的整个内容中添加标题标签<;a>;标签_Php_Xml_Dom_Preg Replace - Fatal编程技术网

Php 在包含标题标签的整个内容中添加标题标签<;a>;标签

Php 在包含标题标签的整个内容中添加标题标签<;a>;标签,php,xml,dom,preg-replace,Php,Xml,Dom,Preg Replace,我有一个动态内容,它几乎没有以“https://”和“http://”开头的标签。我需要为每个以“https”开头的锚标记添加标题。我正在使用的代码无法附加动态标题。例如: $str = '<a href= "https://www.google.com">some text </a> <a href= "https://www.yahoo.com">some other text </a>'; $str='1〕 '; 输出字符串应为: $st

我有一个动态内容,它几乎没有以“https://”和“http://”开头的标签。我需要为每个以“https”开头的锚标记添加标题。我正在使用的代码无法附加动态标题。例如:

$str = '<a href= "https://www.google.com">some text </a>
<a href= "https://www.yahoo.com">some other text </a>';
$str='1〕
';
输出字符串应为:

$str = '<a href= "https://www.google.com" title="dynamic title1">some text </a>
<a href= "https://www.yahoo.com" title="dynamic title2">some text </a>'
$str='1〕
'
我使用的代码是:

$xml = '<foo>'. $content.'</foo>';
$doc = new DOMDocument;
$doc->loadXml($xml);
foreach ($doc->getElementsByTagName('a') as $anchor) {  
    if ($anchor->hasAttribute('title')) {
        $anchor->removeAttribute('title');
    }
}
$newcontent =  $doc->saveHTML();
$pattern = '/(href=("|\')https)(:\\/\\/.*?("|\'))/';
$subject = $newcontent;
preg_match_all ($pattern, $subject, $matches);
for($i=0; $i< count($matches); $i++){
     $complete_url = $matches[0][$i];
     $get_prog_name = explode("//",$complete_url);
     if (strpos($get_prog_name[1],'www.') !== false) {
        $get_prog_name = explode("www.",$complete_url);
      }
         $prog_acro = explode(".", $get_prog_name[1]);

           if($prog_acro[0] == "ccc") {
          $progName = "Dynamic Title 1";
      }
       if($prog_acro[0] == "cec") {
           $progName = "Dynamic Title 2";

      }
   $replacement[] = 'class="tooltip" title=Requires&nbsp;CEB&nbsp;'.$progName.'membership&nbsp;login $1$3';
 } // end foreach loop
 $newstr =  preg_replace($pattern, $replacement[0], $subject, -1 );
$xml=''$内容",;
$doc=新文档;
$doc->loadXml($xml);
foreach($doc->getElementsByTagName('a')作为$anchor){
如果($anchor->hasAttribute('title')){
$anchor->removeAttribute(“标题”);
}
}
$newcontent=$doc->saveHTML();
$pattern='/(href=(“\\”)https)(:\\/\\/.*(“\\\”)/';
$subject=$newcontent;
preg_match_all($pattern,$subject,$matches);
对于($i=0;$i

我想在这里动态替换标题。问题是,当将preg_replace放入循环时,它会多次打印整个内容,因为它包含anhor标记。

不要使用正则表达式匹配/更改XML。您已经使用DOMDocument删除了title属性,为什么不根据href属性使用它来修改/添加它?我真的不明白。你到底想替换什么?根据您的示例,将
“动态标题”
添加到所有
-标记就足够了…@ThW:如何根据href属性修改/添加它。任何代码example@michi:您是对的,我想为每个锚标记添加动态标题您已经在使用hasAttribute()和removeAttribute(),请尝试getAttribute()和setAttribute()。