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()); 
        }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)