当前位置:网站首页>Use preg_ Match extracts the string into the array between: & | people PHP

Use preg_ Match extracts the string into the array between: & | people PHP

2022-07-04 07:53:00 45MA

I'm trying to extract the serial number from the string for matching comparison , And think of using preg_match It could be possible , But I'm struggling with regular expressions .

Can anyone offer any help ?

The current attempts are as follows :

$example = "CPM-200:0123456L|CPM-100:9876543L|CJ Pro:CJP33-011";pre_match("/\:(.*?)\|/", $example, $matches);var_dump($matches);

at present , The above content spits out :

Array(2) {  [0]=> string(10) ":0123456L|"  [1]=> string(8) "0123456L"}

But I actually want to extract it as :

Array(0) {  [0] => 0123456L  [1] => 9876543L  [2] => CJP33-011}

I've never been right regex Be satisfied with ! I tried various combinations , And the above is the closest combination I try to achieve . Need to find a decent online tutorial .

Reference plan

You can use this

:([^|]+)(?:\||$)

Regex Demo

Regular expression decomposition

: #Match :([^|]+) #Match anything other than |(?:  \| #Match | literally    | #Alternation(OR) --> Match | or $   $ #End of string)

PHP Code

$re = "/:([^|]+)(?:\\||$)/"; $str = "CPM-200:0123456L|CPM-100:9876543L|CJ Pro:CJP33-011"; preg_match_all($re, $str, $matches);print_r($matches[1]);

Ideone Demo

Use preg_replace Delete div - php

I'm trying to delete this div<div id="myid" class="myclass">other_tags_here</div> I am using preg_replace ("/<div id=\"myid\" class=\"myclass\">…

PHP- Insert the date into the date time field - php

I have used in the database datetime Field storage date , Use PHP take “ Today's date ” What is the correct way to insert this field ? Let me propose a toast to , Reference plan I think you can use php date() function

PHP- How to get the list of member functions of a class ? - php

If I know the name of the class . Is there any way to know the list of member functions of a class ? Reference plan get_class_methods() It's your friend

PHP- What data should we include in the session ? - php

This is a beginner's problem ... In the website , What type of data should or should not be included in the session ? I understand that I should not include any information that needs to be kept safe . I am more interested in programming best practices . for example , It is possible to include some data in the session , Otherwise, the data will be sent from one page to another as dependency injection . Isn't this equal to creating global variables ? Generally speaking , Which data already exists or does not exist in the session table ? thank you ,JDelage Reference plan You should treat the message as a write …

php ziparchive Class source code - php

Improve this question How do I get ziparchive The source code of the class itself . Reference plan Suppose you are talking about PHP ZipArchive class: download PHP source code And find the appropriate file . If you want the source code to be PHP Code , You may feel disappointed , Because the source code is used C language-written . perhaps , It can also be in PHP Github Development Reposito…

原网站

版权声明
本文为[45MA]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141517534269.html