duanmei1930 2013-11-03 12:08
浏览 52
已采纳

preg_replace第二个参数($ replacement)不合适

The content of my posts in Wordpress is a big markup. It is coming from MS Word so it is text wrapped by HTML nested tags and inline styles.

I have a segment of code that is repeated many times in the content (It represents text footnotes). This segment, for the first footnote for example is:

<sup><a title="" href="file:///C:/Users/hp/Desktop/file.docx#_ftn1" name="_f
tnref1">
 <span class="MsoFootnoteReference">
  <span dir="LTR">
   <span class="MsoFootnoteReference">
    <span lang="EN-US" style="font-size: 16pt; line-height: 115%;">
     [1]
    </span>
   </span>
  </span>
 </span>
</a></sup>
.....

<a title="" href="file:///C:/Users/hp/Desktop/file.docx#_ftnref1" name="_ftn1">
 <span class="MsoFootnoteReference">
  <span dir="LTR" lang="EN-US" style="font-size: 12.0pt; font-family: 'Simplified Arabic','serif';">
   <span class="MsoFootnoteReference">
    <span lang="EN-US" style="font-size: 12pt; line-height: 115%;">
     [1]
    </span>
   </span>
  </span>
 </span>
</a>

My goal is to change the 2 hrefs from:

href="file:///C:/Users/hp/Desktop/file.docx#_ftn1" 

href="file:///C:/Users/hp/Desktop/file.docx#_ftnref1"

to:

href="#_ftn1" 

href="#_ftnref1"

so that the user can jump from one anchor to the other.

The code I am using is:

if(preg_match_all('/href\s*=\s*"[^"]+(#[^"]+)"/',get_the_content(),$match))
{

echo preg_replace('/href\s*=\s*"[^"]+(#[^"]+)"/','href=""', get_the_content()); 
}

Thank you very much in advance for your invaluable assistance.

  • 写回答

1条回答 默认 最新

  • dqyin0101 2013-11-03 12:33
    关注

    Found the solution. Thank you for your time.

       if(preg_match_all('/href\s*=\s*"[^"]+(#[^"]+)"/',get_the_content(),$match))
        {
        echo preg_replace('/href\s*=\s*"[^"]+(#[^"]+)"/','href="$1"', get_the_content()); 
        }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码