Let’s say I have the this text (not to be treated as PHP code):
$this->validation->set('username','username','trim');
$this->validation->set('password','password','trim');
$this->validation->set('password2','password2','trim');
$this->validation->set('name','name','trim');
$this->validation->set('surname','surname','trim');
I want to get the list of first words after set( which is in quotation marks in every line, so the output of previous input must be like this:
username
password
password2
name
surname
I think, it’s possible with regular expressions. My question is how can I get the list of the words which is in first quotation marks with PHP?
Lets say the variable
$textholds the data from your question.Let’s analyse the regular expression
/set\('(.*?)'/:/is the delimiter.set\('and'are the stringsset('and', respectively..*?is the least amount of (arbitrary) characters between the two aforementioned strings.1As a result, this regular expression matches:
$this->validation->set('username','username','trim');To store all the strings you need in the array
$matches[1], we can use the functionpreg_match_all.It suffices to call
preg_match_all("/set\('(.*?)'/", $text, $matches).1 See also: Regex Tutorial – Repetition with Star and Plus – Laziness Instead of Greediness
Example code: