Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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_Php_Regex - Fatal编程技术网

PHP删除#使用preg

PHP删除#使用preg,php,regex,Php,Regex,我需要一个正则表达式来删除twitter名称中的# 我试过这个: $name = '#mytwitter'; $str = preg_replace('/\#/', ' ', $name); 当然,这是一个简单的修复,但谷歌没有帮助。谢谢 你为什么要逃跑 编辑:您的原始代码也可以工作。请注意,preg_replace返回替换的字符串,但不更改原始字符串。$str的值是“mytwitter”。为什么要转义# 编辑:您的原始代码也可以工作。请注意,preg_replace返回替换的字符串,但不更改

我需要一个正则表达式来删除twitter名称中的#

我试过这个:

$name = '#mytwitter';
$str = preg_replace('/\#/', ' ', $name);

当然,这是一个简单的修复,但谷歌没有帮助。谢谢

你为什么要逃跑


编辑:您的原始代码也可以工作。请注意,
preg_replace
返回替换的字符串,但不更改原始字符串。
$str
的值是“mytwitter”。

为什么要转义
#


编辑:您的原始代码也可以工作。请注意,
preg_replace
返回替换的字符串,但不更改原始字符串。
$str
的值是“mytwitter”。

您不需要使用
preg\u replace
,只需使用
str\u replace

str_replace('#','',$name);

您不需要使用
preg\u replace
,只需使用
str\u replace

str_replace('#','',$name);

您不需要转义
#

但是,对于简单的字符删除,最好使用。这种情况下速度更快

$str = str_replace('#', '', $name);

您不需要转义
#

但是,对于简单的字符删除,最好使用。这种情况下速度更快

$str = str_replace('#', '', $name);
我建议您使用它,因为它的性能更好。就这样使用吧:

$str = strtok('#mytwitter', '#');
以下是我刚刚运行的一些基准测试(50000次迭代):

我用于基准测试的脚本是(取自):

我建议使用它,因为它的性能更好。就这样使用吧:

$str = strtok('#mytwitter', '#');
以下是我刚刚运行的一些基准测试(50000次迭代):

我用于基准测试的脚本是(取自):


preg\u replace
str\u replace
慢得多,不需要使用它here@Jaitsu:当然可以,但是在修复和重写代码之间总是有一个权衡。原始海报从中受益最大的一点都不明显。
preg\u replace
str\u replace
慢得多,没有必要使用它here@Jaitsu:当然可以,但是在修复和重写代码之间总是有一个权衡。这是不明显的,在所有的原始海报将受益最大。感谢这一点,将研究它,总是方便,能够加快事情!谢谢你的帮助,我会仔细研究的,总是手到擒来,能够加速事情的发展!
<?php

$number_of_tests = 50000;

// str_replace
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

for($i = 0; $i < $number_of_tests; $i++){
    $str = "#mytwitter";
    $str = str_replace('#' , '', $str);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "strreplace execution time: ".$totaltime." seconds;  <br />";

// preg_replace
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

for($i = 0; $i < $number_of_tests; $i++){
    $str = "#mytwitter";
    $str = preg_replace('/#/', ' ', $str);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "preg_replace execution time: ".$totaltime." seconds;  <br />";

// strtok
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

for($i = 0; $i < $number_of_tests; $i++){
    $str = "#mytwitter";
    $str = strtok($str, "#");
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "strtok execution time: ".$totaltime." seconds; <br />";

  [1]: http://php.net/strtok