트위터 이름에서 #을 제거하려면 정규 표현식이 필요합니다.PHP # 예문을 사용하여 제거 #
이$name = '#mytwitter';
$str = preg_replace('/\#/', ' ', $name);
물론이 간단한 수정이지만, 구글은 도움이되지 않았다 :
이 시도. 감사!트위터 이름에서 #을 제거하려면 정규 표현식이 필요합니다.PHP # 예문을 사용하여 제거 #
이$name = '#mytwitter';
$str = preg_replace('/\#/', ' ', $name);
물론이 간단한 수정이지만, 구글은 도움이되지 않았다 :
이 시도. 감사!당신은 str_replace
를 사용 preg_replace
를 사용할 필요가 없습니다 :
str_replace('#','',$name);
왜 #
을 탈출하고 있습니까?
$name = '#mytwitter';
$str = preg_replace('/#/', ' ', $name);
편집 : 원래 코드도 작업을 수행합니다. preg_replace
은 대체 문자열을 반환하지만 원본은 변경하지 않습니다. $str
의 값은 "mytwitter"입니다.
'preg_replace'는'str_replace'보다 훨씬 느리므로 여기서는 사용할 필요가 없습니다 – JamesHalsall
@Jaitsu : 확실히, 코드 수정과 재 작성 사이에 항상 트레이드 오프가 있습니다. 원래의 포스터가 가장 유익한 것은 분명하지 않습니다. – Tim
당신은 #
을 탈출 할 필요가 없습니다.
$str = preg_replace('/#/', '', $name);
그러나, 간단한 문자 제거, 당신은 str_replace()
를 사용하는 것이 더 낫다. 이러한 상황에서는 더 빠릅니다.
$str = str_replace('#', '', $name);
성과가 좋기 때문에 strtok을 사용하는 것이 좋습니다. 그냥과 같이 사용 :
$str = strtok('#mytwitter', '#');
을 여기에 난 그냥 도망 일부 벤치 마크 (50000 반복)입니다
strreplace execution time: 0.068472146987915 seconds
preg_replace execution time: 0.12657809257507 seconds
strtok execution time: 0.043070077896118 seconds
(Beautiful way to remove GET-variables with PHP?에서 촬영)입니다 내가 벤치 마크에 사용되는 스크립트 :
<?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
가
고마워요, 그걸 들여다 볼께요, 일을 빠르게 할 수있게 항상 편리 해요! – BobFlemming
빠른 수정 후 해결해 주셔서 감사합니다. – BobFlemming