سؤال

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