Products
GG网络技术分享 2025-03-18 16:15 7
I have a string ending with \'*\' or \'^\'.
For example:
$variable = \'abcdef**^^\';
I used preg_split() function to split the string.
$variable = \'abcdef*^^^^\';$words = preg_split(\"/(?<=\\w)\\b[!?.]*/\", $variable, -1, PREG_SPLIT_NO_EMPTY);
And I got the output as
Array(
[0] => abcdef
[1] => *^^^^
)
But the problem begins, when my variable is replaced with \'@\',\'\' and some other special characters.
For Example:
$variable = \'abc@d ef*^^^\';
Output become like this
Array(
[0] => abcd
[1] => @ ef
[2] => *^^^^
)
I need the output like this
Array(
[0] => abcd@ ef
[1] => *^^^^
)
I was trying to replace my current RegEx with RegEx for \'*\' and \'^\', But I can\'t find one.
Thank you for all your answers.
I have figured my solution by the references you all given.
I am posting it,May help anyone in future..
<?php$variable = \'Chloride TDS\';
if (preg_match(\'/[\\*^]/\', $variable))
{
if ( strpos( $variable, \'*\' ) < strpos( $variable, \'^\' ) || strpos( $variable, \'^\' ) === false )
{
$first = strstr($variable, \'*\', true);
$last = strstr($variable, \'*\');
}
if ( strpos( $variable, \'^\' ) < strpos( $variable, \'*\' ) && strpos($variable,\'^\') != \'\' || strpos( $variable, \'*\' ) === false )
{
$first = strstr($variable, \'^\', true);
$last = strstr($variable, \'^\');
}
echo $first;
echo \'<br/>\';
echo $last;
}else{
echo $variable;
}
?>
use \\*
for star. below are the example
<?php$variable = \'abc@d ef*^^^\';
$words = preg_split(\\\"/\\*/\\\", $variable, -1, PREG_SPLIT_NO_EMPTY);
echo \\\"<pre>\\\";
print_r($words);
?>
and it\'s output is this
Array(
[0] => abc@d ef
[1] => ^^^
)
if you want to add * then used this
<?php$variable = \'abc@d ef*^^^\';
$words = preg_split(\\\"/\\*/\\\", $variable, -1, PREG_SPLIT_NO_EMPTY);
$i=0;
foreach ($words as $value) {
if($i != 0)
{
$words[$i]=\'*\'.$words[$i];
}
$i++;
}
echo \\\"<pre>\\\";
print_r($words);
?>
If your split character is just \\\"*\\\" you can use explode:
$words = explode(\'*\', $string, 2);
If it\'s \\\"*\\\" or \\\"^\\\" do this:
$words = preg_split(\\\"/[\\*,\\^]+/\\\", $str, -1, PREG_SPLIT_NO_EMPTY);
If i am grasping true meaning of the question, OP mentioned
I have a string ending with \'*\' or \'^\'.
If you want to split the string in this way, you should change the regexp like this.
$words = preg_split(\\\"/[\\*|\\^]/\\\", $variable, 2, PREG_SPLIT_NO_EMPTY);
This would break the string on first concurrence of * or ^. OP has already excepted an answer, but i am adding mine in case it helps anyone else in this situation.
Edit: You do not need to use preg_split() if you wanna do this. Simply use strstr with a few conditions.
$variable = \'abcd@ ef*\';if ( strpos( $variable, \'*\' ) < strpos( $variable, \'^\' ) || strpos( $variable, \'^\' ) === false )
{
$first = strstr($variable, \'*\', true);
$last = strstr($variable, \'*\');
}
if ( strpos( $variable, \'^\' ) < strpos( $variable, \'*\' ) || strpos( $variable, \'*\' ) === false )
{
$first = strstr($variable, \'^\', true);
$last = strstr($variable, \'^\');
}
echo $first;
echo \'<br/>\';
echo $last;
This one is working on every case.
正则表达式是用于描述字符排列和匹配模式的一种语法规则。它主要用于字符串的模式分割、匹配、查找及替换操作。
1. 用途:匹配、查找、替换、分割
2. php提供了两套正则表达式函数库
*1. Perl 兼容正则表达式函数(推荐使用)
2. POSIX 扩展正则表达式函数
1. 表达式的格式: "/表达式/[修正符]"
解释:其中"/"表示正则表达式的定界符,但是也可以是其他符号:如”#“,”!“
注意:定界符不可以是字母、数字和斜线\\。
像“#”、“|”、“!”等都可以的
如:/.../ #...# |....|
其中修正符是可选的,表示对表达式做额外的修饰。
1. 原子是组成正则表达式的基本单位,在分析正则表达式时,应作为一个整体。
原子包括以下内容:
> 单个字符、数字,如a-z,A-Z,0-9。
> 模式单元,如(ABC)可以理解为由多个原子组成的大的原子。
> 原子表,如 [ABC]。
> 重新使用的模式单元,如:\\\\1
> 普通转义字符,如:\\d, \\D, \\w
> 转义元字符,如:\\*,\\.
> 元字符
2. 元字符(具有特殊意义字符):
[] 表示单个字符的原子表
例如:[aoeiu] 表示任意一个元音字母
[0-9] 表示任意一位数字
[a-z][0-9]表示小写字和一位数字构成的两位字符
[a-zA-Z0-9] 表示任意一位大小字母或数字
[^] 表示除中括号内原子之外的任何字符 是[]的取反
例如:[^0-9] 表示任意一位非数字字符
[^a-z] 表示任意一位非小写字母
{m} 表示对前面原子的数量控制,表示是m次
例如:[0-9]{4} 表示4为数字
[1][3-8][0-9]{9} 手机号码
{m,} 表示对前面原子的数量控制,表示是至少m次
例如: [0-9]{2,} 表示两位及以上的数字
{m,n}表示对前面原子的数量控制,表示是m到n次
例如: [a-z]{6,8} 表示6到8位的小写字母
* 表示对前面原子的数量控制,表示是任意次,等价于{0,}
+ 表示对前面原子的数量控制,表示至少1次,等价于{1,}
? 表示对前面原子的数量控制,表示0次或1次(可有可无) 等价于{0,1}
例如:正整数:[1-9][0-9]*
整数:[\\-]?[0-9]+
email:
() 表示一个整体原子,【还有一个子存储单元的作用】。
也可以使用?:来拒绝子存储。 (?:.*?)
例如:(red) 字串red
(rea|blue) 字串red或blue
(abc){2} 表示两个abc
| 表示或的意思
(rea|blue) 字串red或blue
^ 用在正则单元块的开头处,表示必须以指定的开头
$ 用在正则单元块的结尾处,表示必须以指定的结尾
. 表示任意一个除换行符之外的字符
常用组合: .*? 表示最小匹配所有字符(拒绝贪婪匹配)
3. 普通转义字符:
\\d | 匹配一个数字;等价于[0-9] |
\\D | 匹配除数字以外任何一个字符;等价于[^0-9] |
\\w | 匹配一个英文字母、数字或下划线;等价于[0-9a-zA-Z_] |
\\W | 匹配除英文字母、数字和下划线以外任何一个字符;等价于[^0-9a-zA-Z_] |
\\s | 匹配一个空白字符;等价于[\\f\\n\\r\\t\\v] |
\\S | 匹配除空白字符以外任何一个字符;等价于[^\\f\\n\\r\\t\\v] |
\\f | 匹配一个换页符等价于 \\x0c 或 \\cL |
\\n | 匹配一个换行符;等价于 \\x0a 或 \\cJ |
\\r | 匹配一个回车符等价于\\x0d 或 \\cM |
\\t | 匹配一个制表符;等价于 \\x09\\或\\cl |
\\v | 匹配一个垂直制表符;等价于\\x0b或\\ck |
\\oNN | 匹配一个八进制数字 |
\\xNN | 匹配一个十六进制数字 |
\\cC | 匹配一个控制字符 |
4. 模式修整符
i 表示不区分大小写;
"/[a-zA-Z]/" <==>"/[a-z]/i"
s 表示匹配视为单行(就是可以让点.支持换行)
U 表示拒绝贪婪匹配
preg_grep -- 返回与模式匹配的数组单元
* preg_match_all -- 进行全局正则表达式匹配 , 返回共计匹配的个数。
和下面的一样,不同的是匹配到最后(全局匹配)
* preg_match -- 进行正则表达式匹配,只匹配一次,返回1,否则0,
格式:preg_match("正则表达式","被匹配的字串",存放结果的变量名,PREG_OFFSET_CAPTURE,起始偏移量)
其中:PREG_OFFSET_CAPTURE表示获取匹配索引位置
起始偏移量:从指定位置开始匹配
preg_quote -- 转义正则表达式字符
preg_split -- 用正则表达式分割字符串
preg_replace -- 执行正则表达式的搜索和替换
Demand feedback