Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php使用preg_replace替换此字符串_Php_Replace_Preg Replace - Fatal编程技术网

php使用preg_replace替换此字符串

php使用preg_replace替换此字符串,php,replace,preg-replace,Php,Replace,Preg Replace,我想换这个字符串 第一个单词1/4W可能不同 字符W可以是大写或小写 <?php $str="1/4W this is string"; // 1/4W can be 1/16W, 1/2W, 1W,2w $str=preg_replace(("/^\d.W/", "", $str); var_dump($str); 使用此正则表达式:^\d+(?:/\d+)?w 说明: ~ : regex delimiter ^ : start of string \d+

我想换这个字符串 第一个单词1/4W可能不同 字符W可以是大写或小写

<?php
$str="1/4W this is string"; // 1/4W can be 1/16W, 1/2W, 1W,2w
$str=preg_replace(("/^\d.W/", "", $str);
var_dump($str);
使用此正则表达式:
^\d+(?:/\d+)?w

说明:

~       : regex delimiter
  ^     : start of string
  \d+   : 1 or more digits
  (?:   : start non capture group
    /   : a slash
    \d+ : 1 or more digits
  )?    : end group, optional
  w     : w
~i      : regex delimiter, case insensitive
使用此正则表达式:
^\d+(?:/\d+)?w

说明:

~       : regex delimiter
  ^     : start of string
  \d+   : 1 or more digits
  (?:   : start non capture group
    /   : a slash
    \d+ : 1 or more digits
  )?    : end group, optional
  w     : w
~i      : regex delimiter, case insensitive

在结果中,我在字符串的开头有一个空格,我可以使用trim删除这个空格,但是你可以通过regexi删除空格吗?我已经向regex添加了“~^\d+(?:/\d+)?w\s~i”是否正确?我还有其他问题@user8247367:是,只需在末尾添加
\s
。@user8247367:其他问题请参见我的答案。结果是字符串开头有一个空格,我可以使用trim删除这个空格,但是你可以通过regexi删除空格吗?我让add\s添加到regex“~^\d+(?:/\d+)?w\s~i”正确吗?我还有其他问题@user8247367:是,只需在末尾添加
\s
。@user8247367:其他问题请参见我的答案。