PHP:如何在字符串中查找子字符串的开头和结尾?

问题描述:

这是一个mysql表字段的内容:

This is the content of one mysql table field:

Flash LEDs: 0.5W
LED lamps: 5mm
Low Powers: 0.06W, 0.2W
Remarks(1): this is remark1
----------
Accessories: Light Engine
Lifestyle Lights: Ambion, Crane Fun
Office Lights: OL-Deluxe Series
Street Lights: Dolphin
Retrofits: SL-10A, SL-60A
Remarks(2): this is remark2
----------
Infrared Receiver Module: High Data Rate Short Burst
Optical Sensors: Ambient Light Sensor, Proximity Sensor, RGB Color Sensor
Photo Coupler: Transistor
Remarks(3): this is remark3
----------
Display: Dot Matrix
Remarks(4): this is remark4

现在,我想阅读这些注释并将其存储在变量中.备注(1),备注(2)等是固定的. 'this is remark1'等来自表单输入字段,因此它们很灵活.

Now, I want to read the remarks and store them in a variable. Remarks(1), Remarks(2), etc. are fixed. 'this is remark1', etc. come from form input fields, so they are flexible.

基本上我需要的是:读取'Remarks(1):'和'--------'之间的所有内容并将其保存在变量中.

Basically what I need is: Read everything between 'Remarks(1):' and '--------' and save it in a variable.

感谢您的帮助.

您可以使用正则表达式:

You can use regex:

preg_match_all("~Remarks\(([^)]+)\):([^\n]+)~", $str, $m);

ideone 上看到.

正则表达式会将X放在比赛组1中,将Y放在比赛组2中(备注(X):Y)

The regex will put X in match group 1, Y in match group 2 (Remarks(X): Y)