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

发布于 2024-09-18 15:21:38 字数 716 浏览 2 评论 0原文

这是一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微暖i 2024-09-25 15:21:39

这将是正则表达式的工作,它允许您准确匹配您的要求所表达的规则类型。 这里是给您的教程。

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.

向日葵 2024-09-25 15:21:39

为此使用 preg 函数,否则您可以爆炸和内爆函数以获得正确的结果。不要使用子字符串,它可能无法提供更正。

查询字符串的内爆和爆炸函数示例:

    $sdr = "Remarks(4): this is remark4";

    $sdr1 = explode(":",$sdr);
    $frst = $sdr1[0];

    $sdr2 = array_shift($sdr1);

    $secnd = implode(" ", $sdr1);

    echo "First String - ".$frst;
    echo "<br>";
    echo "Second String - ".$secnd;
    echo "<br>";

您的答案:

First String - Remarks(4)

Second String - this is remark4

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 :

    $sdr = "Remarks(4): this is remark4";

    $sdr1 = explode(":",$sdr);
    $frst = $sdr1[0];

    $sdr2 = array_shift($sdr1);

    $secnd = implode(" ", $sdr1);

    echo "First String - ".$frst;
    echo "<br>";
    echo "Second String - ".$secnd;
    echo "<br>";

Your Answer :

First String - Remarks(4)

Second String - this is remark4
耀眼的星火 2024-09-25 15:21:38

您可以使用正则表达式:

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

ideone 所示。

正则表达式会将 X 放入匹配组 1,将 Y 放入匹配组 2(备注(X): Y)

You can use regex:

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

As seen on ideone.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文