Replace content with regular expressions PHP, regular expression Replacement php_php tutorial

Source: Internet
Author: User

Replace content with regular expressions PHP, regular expressions replace PHP


The previous project was to have a question-and-answer page, such as inserting an expression into the content of a reply or question. Written in the content is the name of the expression image (and is not with the suffix, such as: f_002.png) expression package placed in the project, I need to remove the content when the name of the expression in the image replaced.

The content that is stored in the database is this: I don't know [f_013] (the last [f_013] is the form of emoticons in the database)

All I need to do is take the content out of the database and process the characters like this [f_013] into the image address, and it's clear that regular expressions are needed here.

1. First, I need to find this string first.

" /(?:\ [) (F_. [0-9] {1,3}) (?:\]) /I";

This regular match will help me find the expression string.

2. When you find it, change the string to the address where the expression image is stored, where a function is used to replace the regular expression.

This is the function: Preg_match_all

Echo preg_replace ("/(?: \ [) (F_. [0-9] {1,3}) (?:\]) /I","", $line ['content']);
$line [' content ']   This is what I took out of the database,
"  This is the image address I'm going to replace, like my picture address.
Here is a very important point of knowledge: "\${1}" It is the string of expressions stored in the database.


I have a complete code for my replacement here:

   
     $resultarray();      $n Preg_match_all ("/(?:\ [) (F_. [0-9] {1,3}) (?:\]) /i ",$line$result); /* Return to find the number of strings matching the     criteria */if($n$nfalse)/   * If it is 0 or false, it means no expression.
{
echo$line[' content ']; } Else
{   echopreg_replace("/(?: \ [) (F_. [0-9] {1,3}) (?:\]) /i "," ",$line[' content ']); } ? >

http://www.bkjia.com/PHPjc/1054355.html www.bkjia.com true http://www.bkjia.com/PHPjc/1054355.html techarticle replace content with regular expressions PHP, the regular expression replaces PHP used to do a project, is a question and answer page, such as in the answer or the content of the question insert expression. Write to ...

  • 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.