Pregunta

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);
¿Fue útil?

Solución 2

Using concatenation and preg_quote() :

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

Otros consejos

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top