2012-07-21 3 views
0

배열 $ wordstodelete에 단일 문자가 있으면 $ oneBigDescription의 단어에서 해당 문자가 삭제됩니다. 문자열 내 문자열 제거

$oneBigDescription = str_replace ($wordstodelete, '', $oneBigDescription); 

그래서 그것은 다음과 같이한다 :

array (size=51) 
    'blck' => int 5 
    'centrl' => int 6 
    'clssc' => int 6 
    'club' => int 10 
    'crs' => int 54 
    'deler' => int 7 
    'delers' => int 5 
    'engl' => int 6 
    'felne' => int 8 
    'gude' => int 5 
    'hot' => int 5 
    'jgur' => int 172 
    'jgurs' => int 5 
    'lke' => int 6 

그 자체에있는 경우에만 $ oneBigDescription에서 하나의 문자를 삭제하는 방법이 있나요? 당신이 "검은 색 중심"을 취할 것 좀 정규식을

$oneBigDescription = preg_replace('/\sa\s/', ' ', $oneBigDescription); 

를 사용하고 반환

+3

을 (단어의 목록이 동일하게 유지 할 수 있도록 나는 정규 표현식을 생성하는거야) 귀하의 요청을 이해하기가 어렵습니다. 문제가 무엇이고 필요한 것이 무엇인지 설명하는 예제를 보여줄 수 있습니까? –

답변

2

$oneBigDescription = preg_replace("/\b$wordstodelete\b/", '', $oneBigDescription);

/b이 단어를 "중앙 블랙"보일 것입니다해야 할 수도 있습니다처럼

+0

감사합니다 대우! – flux

0

소리가 난다 한 문자가 사용될 때 분리 된 단어임을 보장하는 경계.

편집 : 제대로 읽지 못했습니다. $ wordstodelete를 단어 배열로 반복한다고 가정합니다. 그래서

, 이런 식으로 뭔가 :

$desc = "blah blah a b blah"; 
$wordstodelete = array("a", "b"); 
foreach($wordstodelete as $delete) 
{ 
    $desc= preg_replace("/\b$delete\b/", "", $desc); 
} 

EDIT2 :이 꽤 행복하지 않았다 약간 정제 :

$arr = "a delete aaa a b me b"; 
$wordstodelete = array("a", "b"); 
$regex = array(); 
foreach($wordstodelete as $word) 
{ 
    $regex[] = "/\b$word\b\s?/"; 
} 
$arr = preg_replace($regex, '', $arr); 

다음과 같은 공간을 복용에 대한이 계정있는 HTML의 경우 일반적으로 연속 된 공백이 렌더링되지 않으므로 렌더링 할 때 문제가되지 않지만 그래도 꺼내는 것이 좋습니다. 이것은 또한 정규식 표현식의 배열을 생성하는데, 이는 약간 더 멋지게 보인다. 당신처럼 "스스로 단어를"단지 대체 preg_replace이다 사용할 수 있습니다

0

다음 : 나는 데

$wordsToReplace = ("a", "foo", "bar", "baz"); 
$regexs = array(); 
foreach ($wordsToReplace as $word) { 

    $regexs[] = "/(\s?)". $word . "\s?/"; 
} 

$oneBigDescription = preg_replace($regexs, '\1', $oneBigDescription);