php - preg_match_all empty matches -
i want match letter , underscore between 2 dollar signs , matches result.
example:
i <a href="$url$">foo</a>
, want have url
or $url$
i've tried following 2 patterns
$pattern = "/\$([a-z\_]*)\$/i"; $pattern = "/\$(.*)\$/i"; // well, that's not want. preg_match_all($pattern, $line, $matches, preg_pattern_order);
okay should work - @ least on regex101. doesn't when test within app. empty test results
array(2) { [0]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } [1]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } } // ...
any ideas?
here sample text use test (i test per line)
<li> <div class="parent"> <a href="$application_url$"> <img preview_image src="$thumbnail$"> <div class="battle_tag">$btag$</div> <div class="class_spec">$spec_one$ / $spec_two$ $class$</div> <div class="item_lvl">ilvl $ilvl$</div> <div class="date">$obtained$</div> </a> </div> </li>
well since i'm not familiar php i'll post code how read actual strings too. maybe i'm doin wrong here.
$file = file($filename); for($i=0; $i<count($file); $i++){ $line = $file[$i]; preg_match_all("/\$([a-z]*)\$/i", $line, $matches); var_dump($matches); echo "<br/>"; }
use single quotes around regex:
preg_match_all('/\$([a-z]*)\$/i', $line, $matches);
maybe better:
preg_match_all('/\$([^\$]+)\$/i', $line, $matches);
single quotes take php out of variable interpolation mode. typically escaping $
within double quoted string enough, apparently not when double quoted string regular expression.