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