Domanda

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);
È stato utile?

Soluzione 2

Using concatenation and preg_quote() :

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

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top