2014-01-27 5 views
0

문장의 처음부터 제거 할 단어 목록이 있습니다. 내가이 예에서는 문자열의 처음부터 단어를 단 한 단어를 제거 할 생각이 코드문장의 시작 부분에서 단어 목록을 제거하는 방법

'안녕하세요'를 사용하여 제거하는 단어의 목록을이 코드를 수정하고 추가 할 수있는 방법

for($i=0; $i<strlen(string); $i++){ 
    $remove = 'Hello'; 
    if (substr(string, 0, strlen($remove)) == $remove) { 
    string = substr(string, strlen($remove));} 
    string=ucfirst(string);} 

input :Hello World. 
output:World. 

코드 한번? 어쩌면 단어의 배열이 좋을지도 모릅니다.

또한이 코드를 제거해야하는 각 단어에 대해 여러 번 사용할 수 있지만 성능면에서 느린 것으로 간주됩니다. 어떤 도움? thanks

답변

0

PHP preg_replace 함수를 사용해보십시오.

$string = 'Hello world Hello'; 

$patterns = array(); 
$patterns[0] = '/Hello/'; 

$replacements = array(); 
$replacements[0] = ''; 

ksort($patterns); 
ksort($replacements); 

echo preg_replace($patterns, $replacements, $string, 1); 

참조 : http://www.php.net/manual/en/function.preg-replace.php

+0

나는 문장의 첫 단어 만 제거하고 싶습니다. – mwweb

+0

답변을 업데이트했습니다. 이럴 수있어. – Duli

0

내가 배열에 문자열을 폭발 할 것이다.

$string = 'these are words some of which may be bad like this1 this2 and this3'; 

$words = explode(' ', $string); 

foreach($words as $word) { 

    // Insert function to check your list of words against an array remove 
    // and even replace them with your own. 
    // View: preg_replace http://us2.php.net/manual/en/function.preg-replace.php 

} 
관련 문제