phphtmlregexwordpress

preg_replace second parameter ($replacement) doesnt fit


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.


Solution

  •    if(preg_match_all('/href\s*=\s*"[^"]+(#[^"]+)"/',get_the_content(),$match))
        {
        echo preg_replace('/href\s*=\s*"[^"]+(#[^"]+)"/','href="$1"', get_the_content()); 
        }