Extract and replace strings using preg_match regular expressions in wordpress

Source: Internet
Author: User
Tags regular expression

Run the following code:

The code is as follows: Copy code

Width = "60" height = "60" class = "alignnone size-full wp-image-2100"/>

When the output is displayed, replace it:

The code is as follows: Copy code

<Div class = "gif-box">
_ Src = "/wp-content/uploads/2014/02/hbzy1.jpg! Static"
Class = "alignnones size-full wp-image-2100"
Tsrc = "/wp-content/uploads/2014/02/hbzy1.jpg" width = "60" height = "60"/>
<Div class = "gif-loading-box">
<I class = "gif-loading" style = "display: none;"> </I>
<I class = "gif-play"> </I>
</Div>
</Div>

This is to enable the dynamic GIF image to be played only when it is clicked by default. In this case, preg_match and regular expression are used to match the image code, and str_replace is used to replace and modify strings.

The code is as follows: Copy code

Function gif_content ($ content ){
$ Content = get_the_content ();
$ Img_html = preg_match ('/(] +>)/I', $ content, $ maches); // Obtain the img Image html, such: // www.111cn.net
$ Img_html = $ maches [0];
 
$ Img_width = preg_match ('/width = "[0-9] +"/I', $ img_html, $ maches); // Obtain the image width, for example: width = "32"
$ Img_width = str_replace ('=', ':', $ maches [0]);
$ Img_width = str_replace ('"','', $ img_width );
$ Img_width = $ img_width. 'Px '; // replace the value with width: 32px.
 
$ Img_height = preg_match ('/height = "[0-9] +"/I', $ img_html, $ maches );
$ Img_height = str_replace ('=', ':', $ maches [0]);
$ Img_height = str_replace ('"','', $ img_height );
$ Img_height = $ img_height. 'Px ';
 
$ Img_name = preg_match ('! Http: //. + .(? : Jpe? G | png | gif )! Ui ', $ img_html, $ maches );
$ Img_name = $ maches [0];
 
$ Img_html2 = str_replace ('src = "'. $ img_name.'" ', 'src = "'. $ img_name .'! Static "_ src =" '. $ img_name .'! Static '.' "tsrc =" '. $ img_name.' "', $ img_html );
$ Img_html2 = '<div class = "gif-box" style = "'. $ img_width. ';'. $ img_height. '"> '. $ img_html2. '<div class = "gif-loading-box"> <I class = "gif-loading" style = "display: none; "> </I> <I class =" gif-play "> </I> </div> '; // add a div before and after the html code of the image
 
$ Content = str_replace ($ img_html, $ img_html2, $ content );
Return $ content;
}
 
Add_filter ('The _ content', 'gif _ content', 10); // hook function of wordpress

This is used in wordpress document form. When the document form is image, perform this operation for the image in this document form.

In addition, use the following code in index. php:

The code is as follows: Copy code
$ Format = get_post_format ();
If ('image '! = $ Format ){
Remove_filter ('The _ content', 'gif _ content ');
}
Get_template_part ('content', get_post_format ());

Here, we need to determine if this hook is removed from other document forms, otherwise all document images will be subjected to such operations.

This article describes how to extract and replace strings using the preg_match regular expression and the article form of wordpress.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.