1
votes

How do I get all words starting with $ (it's not a variable)?

This is what I've got, but it doesn't return the words. Can anyone help me?

$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);
2
preg_match_all and proper quotingYour Common Sense

2 Answers

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

preg_match_all to fin ALL occurrences, single quotes to make \$ be correctly read.

'/(\$\w+)/'

might be even better.

0
votes

preg_match_all()? (Like in here)