I’m still learning PHP Regex so I’m hoping someone can help me with what I’m trying to accomplish.
$string = 'Writing to tell you that var MyCode = "dentline"; Learn it.';
What I’d like to do is match the part of the string which reads
var MyCode ="
After I match that part, I want to retrieve the rest of the dynamically generated characters that follow that string. In this example, [dentline] is 8 characters, but this may not always be the case. Therefore, I want to match all the way until I reach
";
After I’ve effectively captured that part of the string, I want to strip the string so the remaining information is what lies between the double quotes
dentline
Any help is much appreciated!
Try this:
Result:
ideone
Explanation
The capturing group “captures” the contents of the match and stores it in the array
$matchesso that it can be accessed afterwards.More information about these constructs can be found here:
Variations
If “MyCode” can vary then use this instead:
In this expression
\wmeans “match any word character”. You might also want to use\s+instead of a space so that you can match one or more of any whitespace characters (also tab and new line). Similarly\s*matches zero or more whitespace. So another possibility for you to try is this: