Вопрос

I am using the following to replace / shorten a string which works fine so far. How can I use a variable here (e.g. $myVar) instead of the hard-coded search term (mySearchTerm) ?

preg_match('/mySearchTerm\:\s*([^\_]+)/', $myString, $out);
Это было полезно?

Решение 2

Using concatenation and preg_quote() :

$term = 'mySearchTerm';
preg_match('/' . preg_quote($term, '/') . ':\s*([^_]+)/', $myString, $out);

Другие советы

If you do the below, the first bit can be any text you want. You must use any characters followed by the :

preg_match('/([^:]+):\s*([^_]+)/', $myString, $out);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top