Home >Backend Development >PHP Tutorial >PHP regular expression to get part of the start string and end string

PHP regular expression to get part of the start string and end string

WBOY
WBOYOriginal
2016-07-29 09:00:172131browse

In discuz, the content in the message field in the reply table (forum_post) is saved in the following form:

$message = "[i=s] u672cu5e16u6700u540eu7531 areyouok u4e8e 2016-3-5 15:12 u7f16u8f91 [/i ]nn[attach]41[/attach][attach]10[/attach]u592au9633u5149uff0cu91d1u4eaeu4eaeuff0cu96c4u9e21u9ad8u9ad8u5531rn";

The part between [attach] and [/attach] is the attachment ID. I need to get it and find the content of the specific attachment.

Here you need to use php regular expressions to get the content between the start string and the end string, and put it into an array

Some codes that passed the test:

//Get the start character A character after the string
$subject = '"./uploads/meinian/96140234039638604.html"n"./uploads/meinian/json_30_60393801_20140924.txt"';
$pattern = '#"./uploads/meinian/(.*?)"#i';
$items = preg_match_all($pattern, $subject, $matches);
var_dump($matches);
echo '< ;hr/>';
//Get the part between the start string and the end string
$subject = "[i=s] u672cu5e16u6700u540eu7531 areyouok u4e8e 2016-3-5 15:12 u7f16u8f91 [/i]nn[ attach]41[/attach][attach]10[/attach]u592au9633u5149uff0cu91d1u4eaeu4eaeuff0cu96c4u9e21u9ad8u9ad8u5531rn";
$pattern = '#[attach](.*?)[\/attach]#i';
$items = preg_match_all($pattern tern, $subject, $matches);
$target = preg_replace($pattern,'',$subject);
echo 'target='.$target.'


';
var_dump($matches);
echo '
';
?>

The above introduces the PHP regular expression to obtain part of the start string and end string, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn