Question

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);
Was it helpful?

Solution 2

Using concatenation and preg_quote() :

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

OTHER TIPS

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top