PHP:如何在字符串中查找子字符串的开头和结尾? [英] PHP: How to find the beginning and end of a substring in a string?

查看:307
本文介绍了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)

这篇关于PHP:如何在字符串中查找子字符串的开头和结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆