PHP:如何查找字符串中子字符串的开头和结尾?
这是一个mysql表字段的内容:
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)等是固定的。 '这是remark1'等来自表单输入字段,因此它们很灵活。
基本上我需要的是:读取“备注(1):”和“--------”之间的所有内容并将其保存在变量中。
感谢您的帮助。
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
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.
Basically what I need is: Read everything between 'Remarks(1):' and '--------' and save it in a variable.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将是正则表达式的工作,它允许您准确匹配您的要求所表达的规则类型。 这里是给您的教程。
This would be a job for regular expressions, which allow you to match on exactly the kinds of rules your requirements express. Here is a tutorial for you.
为此使用
preg
函数,否则您可以爆炸和内爆函数以获得正确的结果。不要使用子字符串,它可能无法提供更正。查询字符串的内爆和爆炸函数示例:
您的答案:
Use
preg
function for this or otherwise you can explode and implode function to get correct result. Don't Use Substring it may not provide correction.Example of Implode and Explode Function for your query string :
Your Answer :
您可以使用正则表达式:
如 ideone 所示。
正则表达式会将 X 放入匹配组 1,将 Y 放入匹配组 2(备注(X): Y)
You can use regex:
As seen on ideone.
The regex will put X in match group 1, Y in match group 2 (Remarks(X): Y)