Variable inside string

Lets say I have a string "My name is $name and my pet is $animal"

How to check if string has variables inside it? And if has, add to array like

$array = ("$name","animal");

Would it be some pregmatch? but then all $+sometextafterthesymbol needs to be extracted and $ with space after it left alone. Any ideas?


You can use regular expressions for this. The following will match any dollar sign followed by 1 or more word characters (letters, numbers, or underscore):

preg_match_all('/\$(\w+)/', $string, $matches);


    [0] => Array
            [0] => $name
            [1] => $animal

    [1] => Array
            [0] => name
            [1] => animal


Remember that $string, if hardcoded, must be wrapped in single quotes (').

Need Your Help

Using setObject() instead of all data types in ojdbc

java oracle prepared-statement ojdbc

can I use setObject() method of PreparedStatement for all datatypes(like string, int, double..) for Oracle database and in its OJDBC?

How to implement a drag and drop menu on an html page?

jquery drag-and-drop

i never did something like this so i was guessing how to do it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.