2012-02-06 2 views
1

$로 시작하는 모든 단어를 얻는 방법 (변수가 아닙니다)?

이것은 내가 얻었지만 단어를 반환하지는 않습니다. 누구든지 나를 도울 수 있습니까?

 
$str = 'Lorem $ipsum dolor sit $amet, consectetur adipiscing $elit. Fusce nec turpis magna, in tempor sem. Duis porttitor $feugiat ligula, in vehicula nunc pellentesque vel. Morbi consectetur pretium lorem. Vivamus nunc mauris, consequat vel condimentum vel, lobortis sit amet eros. Fusce hendrerit adipiscing justo at scelerisque. $Phasellus sed convallis $tellus. In sed lacus ac nulla bibendum scelerisque.'; 

preg_match("/(\$[a-z]+)/i", $str, $matches); 
var_dump($matches); 
+0

preg_match _ ** 모두 ** 그리고 적절한 인용문 –

답변

3
preg_match_all('/(\$[a-z]+)/i', $str, $matches); 

preg_match_all는 작은 따옴표가 \$이 제대로 읽을 수 있도록, 모든 항목을 지느러미합니다.

'/(\$\w+)/' 

더 좋을 수도 있습니다.

+0

동일한 데이터를 가진'$ matches'에서 두 개의 배열을 얻습니다. http://www.pastelib.com/show/9aEYxHwN을 참조하십시오 이유는 무엇입니까? – Marwelln

+0

물론, 당신은 (당신의 OQ에서) 괄호를 사용하고 있습니다 - 원하지 않는다면''/ \ $ \ w +/'' –

0

preg_match_all()? (에서 here처럼)

+0

큰 따옴표 안에'\ $'은 영향을받지 않습니다. –

+0

@EugenRieck 맞아요! –

관련 문제